Skip to content

PYTHON-1326 Remove the "useCursor" aggregate option #560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Breaking Changes in 4.0
- Removed :meth:`pymongo.collection.Collection.remove`.
- Removed :meth:`pymongo.collection.Collection.find_and_modify`.
- Removed :meth:`pymongo.collection.Collection.group`.
- Removed the ``useCursor`` option for
:meth:`~pymongo.collection.Collection.aggregate`.
- Removed :meth:`pymongo.mongo_client.MongoClient.close_cursor`. Use
:meth:`pymongo.cursor.Cursor.close` instead.
- Removed :meth:`pymongo.mongo_client.MongoClient.kill_cursors`.
Expand Down
8 changes: 8 additions & 0 deletions doc/migrate-to-pymongo4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ can be changed to this::
Collection
----------

The useCursor option for Collection.aggregate is removed
........................................................

Removed the ``useCursor`` option for
:meth:`~pymongo.collection.Collection.aggregate` which was deprecated in
PyMongo 3.6. The option was only necessary when upgrading from MongoDB 2.4
to MongoDB 2.6.

Collection.insert is removed
............................

Expand Down
11 changes: 1 addition & 10 deletions pymongo/aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,6 @@ def get_cursor(self, session, server, sock_info, slave_ok):


class _CollectionAggregationCommand(_AggregationCommand):
def __init__(self, *args, **kwargs):
# Pop additional option and initialize parent class.
use_cursor = kwargs.pop("use_cursor", True)
super(_CollectionAggregationCommand, self).__init__(*args, **kwargs)

# Remove the cursor document if the user has set use_cursor to False.
self._use_cursor = use_cursor
if not self._use_cursor:
self._options.pop("cursor", None)

@property
def _aggregation_target(self):
Expand All @@ -201,7 +192,7 @@ def __init__(self, *args, **kwargs):
super(_CollectionRawAggregationCommand, self).__init__(*args, **kwargs)

# For raw-batches, we set the initial batchSize for the cursor to 0.
if self._use_cursor and not self._performs_write:
if not self._performs_write:
self._options["cursor"]["batchSize"] = 0


Expand Down
19 changes: 4 additions & 15 deletions pymongo/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2124,19 +2124,9 @@ def options(self, session=None):

def _aggregate(self, aggregation_command, pipeline, cursor_class, session,
explicit_session, **kwargs):
# Remove things that are not command options.
use_cursor = True
if "useCursor" in kwargs:
warnings.warn(
"The useCursor option is deprecated "
"and will be removed in PyMongo 4.0",
DeprecationWarning, stacklevel=2)
use_cursor = common.validate_boolean(
"useCursor", kwargs.pop("useCursor", True))

cmd = aggregation_command(
self, cursor_class, pipeline, kwargs, explicit_session,
user_fields={'cursor': {'firstBatch': 1}}, use_cursor=use_cursor)
user_fields={'cursor': {'firstBatch': 1}})
return self.__database.client._retryable_read(
cmd.get_cursor, cmd.get_read_preference(session), session,
retryable=not cmd._performs_write)
Expand All @@ -2155,13 +2145,10 @@ def aggregate(self, pipeline, session=None, **kwargs):
- `maxTimeMS` (int): The maximum amount of time to allow the operation
to run in milliseconds.
- `batchSize` (int): The maximum number of documents to return per
batch. Ignored if the connected mongod or mongos does not support
returning aggregate results using a cursor, or `useCursor` is
``False``.
batch.
- `collation` (optional): An instance of
:class:`~pymongo.collation.Collation`. This option is only supported
on MongoDB 3.4 and above.
- `useCursor` (bool): Deprecated. Will be removed in PyMongo 4.0.

The :meth:`aggregate` method obeys the :attr:`read_preference` of this
:class:`Collection`, except when ``$out`` or ``$merge`` are used, in
Expand All @@ -2186,6 +2173,8 @@ def aggregate(self, pipeline, session=None, **kwargs):
A :class:`~pymongo.command_cursor.CommandCursor` over the result
set.

.. versionchanged:: 4.0
Removed the ``useCursor`` option.
.. versionchanged:: 3.9
Apply this collection's read concern to pipelines containing the
`$out` stage when connected to MongoDB >= 4.2.
Expand Down
16 changes: 2 additions & 14 deletions test/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1615,12 +1615,7 @@ def test_aggregate(self):
self.assertRaises(TypeError, db.test.aggregate, "wow")

pipeline = {"$project": {"_id": False, "foo": True}}
# MongoDB 3.5.1+ requires either the 'cursor' or 'explain' options.
if client_context.version.at_least(3, 5, 1):
result = db.test.aggregate([pipeline])
else:
result = db.test.aggregate([pipeline], useCursor=False)

result = db.test.aggregate([pipeline])
self.assertTrue(isinstance(result, CommandCursor))
self.assertEqual([{'foo': [1, 2]}], list(result))

Expand All @@ -1639,11 +1634,7 @@ def test_aggregate_raw_bson(self):
coll = db.get_collection(
'test',
codec_options=CodecOptions(document_class=RawBSONDocument))
# MongoDB 3.5.1+ requires either the 'cursor' or 'explain' options.
if client_context.version.at_least(3, 5, 1):
result = coll.aggregate([pipeline])
else:
result = coll.aggregate([pipeline], useCursor=False)
result = coll.aggregate([pipeline])
self.assertTrue(isinstance(result, CommandCursor))
first_result = next(result)
self.assertIsInstance(first_result, RawBSONDocument)
Expand All @@ -1655,9 +1646,6 @@ def test_aggregation_cursor_validation(self):
cursor = db.test.aggregate([projection], cursor={})
self.assertTrue(isinstance(cursor, CommandCursor))

cursor = db.test.aggregate([projection], useCursor=True)
self.assertTrue(isinstance(cursor, CommandCursor))

def test_aggregation_cursor(self):
db = self.db
if client_context.has_secondaries:
Expand Down