From 4169a048213d746e3267512de7abaca9c1c7894c Mon Sep 17 00:00:00 2001 From: Shane Harvey Date: Fri, 22 Mar 2019 10:57:53 -0700 Subject: [PATCH] PYTHON-1786 Send comment with Cursor.count and Cursor.distinct --- doc/changelog.rst | 5 +++++ pymongo/collection.py | 7 ++++--- pymongo/cursor.py | 7 ++++--- test/test_cursor.py | 8 ++------ 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/doc/changelog.rst b/doc/changelog.rst index 6a0f63487..29481f21f 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -77,6 +77,11 @@ Changes in Version 3.8.0.dev0 the :class:`~bson.codec_options.TypeCodec` and :class:`~bson.codec_options.TypeRegistry` APIs. For more information, see the :doc:`custom type example `. +- :meth:`pymongo.cursor.Cursor.distinct` and + :meth:`pymongo.cursor.Cursor.count` now send the Cursor's + :meth:`~pymongo.cursor.Cursor.comment` as the "comment" top-level + command option instead of "$comment". Also, note that "comment" must be a + string. Issues Resolved diff --git a/pymongo/collection.py b/pymongo/collection.py index 0cfce8081..a7a144254 100644 --- a/pymongo/collection.py +++ b/pymongo/collection.py @@ -1373,9 +1373,10 @@ class Collection(common.BaseObject): exclusive upper bound for all keys of a specific index in order. Pass this as an alternative to calling :meth:`~pymongo.cursor.Cursor.max` on the cursor. - - `comment` (optional): A string or document. Pass this as an - alternative to calling :meth:`~pymongo.cursor.Cursor.comment` on the - cursor. + - `comment` (optional): A string to attach to the query to help + interpret and trace the operation in the server logs and in profile + data. Pass this as an alternative to calling + :meth:`~pymongo.cursor.Cursor.comment` on the cursor. - `modifiers` (optional): **DEPRECATED** - A dict specifying additional MongoDB query modifiers. Use the keyword arguments listed above instead. diff --git a/pymongo/cursor.py b/pymongo/cursor.py index 51b105b9e..2f541dbdf 100644 --- a/pymongo/cursor.py +++ b/pymongo/cursor.py @@ -754,7 +754,7 @@ class Cursor(object): if self.__max_time_ms is not None: cmd["maxTimeMS"] = self.__max_time_ms if self.__comment: - cmd["$comment"] = self.__comment + cmd["comment"] = self.__comment if self.__hint is not None: cmd["hint"] = self.__hint @@ -791,7 +791,7 @@ class Cursor(object): if self.__max_time_ms is not None: options['maxTimeMS'] = self.__max_time_ms if self.__comment: - options['$comment'] = self.__comment + options['comment'] = self.__comment if self.__collation is not None: options['collation'] = self.__collation @@ -862,7 +862,8 @@ class Cursor(object): http://docs.mongodb.org/manual/reference/operator/comment/ :Parameters: - - `comment`: A string or document + - `comment`: A string to attach to the query to help interpret and + trace the operation in the server logs and in profile data. .. versionadded:: 2.7 """ diff --git a/test/test_cursor.py b/test/test_cursor.py index 6f148bab5..ce85a1bd3 100644 --- a/test/test_cursor.py +++ b/test/test_cursor.py @@ -1218,12 +1218,8 @@ class TestCursor(IntegrationTest): self.assertTrue(c1.alive) @client_context.require_no_mongos - @client_context.require_version_max(4, 1, 8) @ignore_deprecations def test_comment(self): - if client_context.auth_enabled: - raise SkipTest("SERVER-4754 - This test uses profiling.") - # MongoDB 3.1.5 changed the ns for commands. regex = {'$regex': r'pymongo_test.(\$cmd|test)'} @@ -1247,14 +1243,14 @@ class TestCursor(IntegrationTest): op = self.db.system.profile.find({'ns': regex, 'op': 'command', 'command.count': 'test', - 'command.$comment': 'foo'}) + 'command.comment': 'foo'}) self.assertEqual(op.count(), 1) self.db.test.find().comment('foo').distinct('type') op = self.db.system.profile.find({'ns': regex, 'op': 'command', 'command.distinct': 'test', - 'command.$comment': 'foo'}) + 'command.comment': 'foo'}) self.assertEqual(op.count(), 1) finally: self.db.set_profiling_level(OFF)