Skip to content

Commit 4169a04

Browse files
committed
PYTHON-1786 Send comment with Cursor.count and Cursor.distinct
1 parent c55a662 commit 4169a04

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

doc/changelog.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ Changes in Version 3.8.0.dev0
7777
the :class:`~bson.codec_options.TypeCodec` and
7878
:class:`~bson.codec_options.TypeRegistry` APIs. For more information, see
7979
the :doc:`custom type example <examples/custom_type>`.
80+
- :meth:`pymongo.cursor.Cursor.distinct` and
81+
:meth:`pymongo.cursor.Cursor.count` now send the Cursor's
82+
:meth:`~pymongo.cursor.Cursor.comment` as the "comment" top-level
83+
command option instead of "$comment". Also, note that "comment" must be a
84+
string.
8085

8186

8287
Issues Resolved

pymongo/collection.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,9 +1373,10 @@ def find(self, *args, **kwargs):
13731373
exclusive upper bound for all keys of a specific index in order.
13741374
Pass this as an alternative to calling
13751375
:meth:`~pymongo.cursor.Cursor.max` on the cursor.
1376-
- `comment` (optional): A string or document. Pass this as an
1377-
alternative to calling :meth:`~pymongo.cursor.Cursor.comment` on the
1378-
cursor.
1376+
- `comment` (optional): A string to attach to the query to help
1377+
interpret and trace the operation in the server logs and in profile
1378+
data. Pass this as an alternative to calling
1379+
:meth:`~pymongo.cursor.Cursor.comment` on the cursor.
13791380
- `modifiers` (optional): **DEPRECATED** - A dict specifying
13801381
additional MongoDB query modifiers. Use the keyword arguments listed
13811382
above instead.

pymongo/cursor.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ def count(self, with_limit_and_skip=False):
754754
if self.__max_time_ms is not None:
755755
cmd["maxTimeMS"] = self.__max_time_ms
756756
if self.__comment:
757-
cmd["$comment"] = self.__comment
757+
cmd["comment"] = self.__comment
758758

759759
if self.__hint is not None:
760760
cmd["hint"] = self.__hint
@@ -791,7 +791,7 @@ def distinct(self, key):
791791
if self.__max_time_ms is not None:
792792
options['maxTimeMS'] = self.__max_time_ms
793793
if self.__comment:
794-
options['$comment'] = self.__comment
794+
options['comment'] = self.__comment
795795
if self.__collation is not None:
796796
options['collation'] = self.__collation
797797

@@ -862,7 +862,8 @@ def comment(self, comment):
862862
http://docs.mongodb.org/manual/reference/operator/comment/
863863
864864
:Parameters:
865-
- `comment`: A string or document
865+
- `comment`: A string to attach to the query to help interpret and
866+
trace the operation in the server logs and in profile data.
866867
867868
.. versionadded:: 2.7
868869
"""

test/test_cursor.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,12 +1218,8 @@ def test_with_statement(self):
12181218
self.assertTrue(c1.alive)
12191219

12201220
@client_context.require_no_mongos
1221-
@client_context.require_version_max(4, 1, 8)
12221221
@ignore_deprecations
12231222
def test_comment(self):
1224-
if client_context.auth_enabled:
1225-
raise SkipTest("SERVER-4754 - This test uses profiling.")
1226-
12271223
# MongoDB 3.1.5 changed the ns for commands.
12281224
regex = {'$regex': r'pymongo_test.(\$cmd|test)'}
12291225

@@ -1247,14 +1243,14 @@ def test_comment(self):
12471243
op = self.db.system.profile.find({'ns': regex,
12481244
'op': 'command',
12491245
'command.count': 'test',
1250-
'command.$comment': 'foo'})
1246+
'command.comment': 'foo'})
12511247
self.assertEqual(op.count(), 1)
12521248

12531249
self.db.test.find().comment('foo').distinct('type')
12541250
op = self.db.system.profile.find({'ns': regex,
12551251
'op': 'command',
12561252
'command.distinct': 'test',
1257-
'command.$comment': 'foo'})
1253+
'command.comment': 'foo'})
12581254
self.assertEqual(op.count(), 1)
12591255
finally:
12601256
self.db.set_profiling_level(OFF)

0 commit comments

Comments
 (0)