Skip to content

Commit c936e18

Browse files
hensomA. Jesse Jiryu Davis
authored andcommitted
Cursors now support setting comments
1 parent 3757086 commit c936e18

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

pymongo/cursor.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ def __init__(self, collection, spec=None, fields=None, skip=0, limit=0,
145145
self.__max_scan = max_scan
146146
self.__explain = False
147147
self.__hint = None
148+
self.__comment = None
148149
self.__as_class = as_class
149150
self.__slave_okay = slave_okay
150151
self.__manipulate = manipulate
@@ -271,6 +272,8 @@ def __query_spec(self):
271272
operators["$explain"] = True
272273
if self.__hint:
273274
operators["$hint"] = self.__hint
275+
if self.__comment:
276+
operators["$comment"] = self.__comment
274277
if self.__snapshot:
275278
operators["$snapshot"] = True
276279
if self.__max_scan:
@@ -657,6 +660,8 @@ def count(self, with_limit_and_skip=False):
657660
command['_use_master'] = use_master
658661
if self.__max_time_ms is not None:
659662
command["maxTimeMS"] = self.__max_time_ms
663+
if self.__comment:
664+
command['$comment'] = self.__comment
660665

661666
if with_limit_and_skip:
662667
if self.__limit:
@@ -715,6 +720,8 @@ def distinct(self, key):
715720
options['_use_master'] = use_master
716721
if self.__max_time_ms is not None:
717722
options['maxTimeMS'] = self.__max_time_ms
723+
if self.__comment:
724+
options['$comment'] = self.__comment
718725

719726
database = self.__collection.database
720727
return database.command("distinct",
@@ -766,6 +773,17 @@ def hint(self, index):
766773
self.__hint = helpers._index_document(index)
767774
return self
768775

776+
def comment(self, comment):
777+
"""Adds a 'comment', to the cursor.
778+
779+
http://docs.mongodb.org/manual/reference/operator/comment/
780+
781+
:Parameters:
782+
- `comment`: A string or document
783+
"""
784+
self.__comment = comment
785+
return self
786+
769787
def where(self, code):
770788
"""Adds a $where clause to this query.
771789

test/test_cursor.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
from bson.code import Code
2929
from bson.son import SON
3030
from pymongo import (ASCENDING,
31-
DESCENDING)
31+
DESCENDING,
32+
ALL,
33+
OFF)
3234
from pymongo.database import Database
3335
from pymongo.errors import (InvalidOperation,
3436
OperationFailure,
@@ -38,6 +40,7 @@
3840
from test import version
3941
from test.test_client import get_client
4042
from test.utils import is_mongos, get_command_line
43+
from contextlib import contextmanager
4144

4245

4346
class TestCursor(unittest.TestCase):
@@ -939,5 +942,37 @@ def test_with_statement(self):
939942
"""
940943
self.assertTrue(c1.alive)
941944

945+
def test_comment(self):
946+
@contextmanager
947+
def new_profile():
948+
self.db.set_profiling_level(OFF)
949+
self.db.system.profile.drop()
950+
self.db.set_profiling_level(ALL)
951+
yield
952+
self.db.set_profiling_level(OFF)
953+
954+
with new_profile():
955+
matching = list(self.db.test.find({'type': 'string'}).comment('foo'))
956+
op = self.db.system.profile.find({'ns': 'pymongo_test.test',
957+
'op': 'query',
958+
'query.$comment': 'foo'})
959+
self.assertEqual(op.count(), 1)
960+
961+
with new_profile():
962+
self.db.test.find({'type': 'string'}).comment('foo').count()
963+
op = self.db.system.profile.find({'ns': 'pymongo_test.$cmd',
964+
'op': 'command',
965+
'command.count': 'test',
966+
'command.$comment': 'foo'})
967+
self.assertEqual(op.count(), 1)
968+
969+
with new_profile():
970+
self.db.test.find({'type': 'string'}).comment('foo').distinct('type')
971+
op = self.db.system.profile.find({'ns': 'pymongo_test.$cmd',
972+
'op': 'command',
973+
'command.distinct': 'test',
974+
'command.$comment': 'foo'})
975+
self.assertEqual(op.count(), 1)
976+
942977
if __name__ == "__main__":
943978
unittest.main()

0 commit comments

Comments
 (0)