Skip to content

Commit ab35e0d

Browse files
authored
PYTHON-1326 Remove the "useCursor" aggregate option (#560)
1 parent ac4bacb commit ab35e0d

File tree

5 files changed

+17
-39
lines changed

5 files changed

+17
-39
lines changed

doc/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ Breaking Changes in 4.0
3636
- Removed :meth:`pymongo.collection.Collection.remove`.
3737
- Removed :meth:`pymongo.collection.Collection.find_and_modify`.
3838
- Removed :meth:`pymongo.collection.Collection.group`.
39+
- Removed the ``useCursor`` option for
40+
:meth:`~pymongo.collection.Collection.aggregate`.
3941
- Removed :meth:`pymongo.mongo_client.MongoClient.close_cursor`. Use
4042
:meth:`pymongo.cursor.Cursor.close` instead.
4143
- Removed :meth:`pymongo.mongo_client.MongoClient.kill_cursors`.

doc/migrate-to-pymongo4.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ can be changed to this::
140140
Collection
141141
----------
142142

143+
The useCursor option for Collection.aggregate is removed
144+
........................................................
145+
146+
Removed the ``useCursor`` option for
147+
:meth:`~pymongo.collection.Collection.aggregate` which was deprecated in
148+
PyMongo 3.6. The option was only necessary when upgrading from MongoDB 2.4
149+
to MongoDB 2.6.
150+
143151
Collection.insert is removed
144152
............................
145153

pymongo/aggregation.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,6 @@ def get_cursor(self, session, server, sock_info, slave_ok):
169169

170170

171171
class _CollectionAggregationCommand(_AggregationCommand):
172-
def __init__(self, *args, **kwargs):
173-
# Pop additional option and initialize parent class.
174-
use_cursor = kwargs.pop("use_cursor", True)
175-
super(_CollectionAggregationCommand, self).__init__(*args, **kwargs)
176-
177-
# Remove the cursor document if the user has set use_cursor to False.
178-
self._use_cursor = use_cursor
179-
if not self._use_cursor:
180-
self._options.pop("cursor", None)
181172

182173
@property
183174
def _aggregation_target(self):
@@ -201,7 +192,7 @@ def __init__(self, *args, **kwargs):
201192
super(_CollectionRawAggregationCommand, self).__init__(*args, **kwargs)
202193

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

207198

pymongo/collection.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,19 +2124,9 @@ def options(self, session=None):
21242124

21252125
def _aggregate(self, aggregation_command, pipeline, cursor_class, session,
21262126
explicit_session, **kwargs):
2127-
# Remove things that are not command options.
2128-
use_cursor = True
2129-
if "useCursor" in kwargs:
2130-
warnings.warn(
2131-
"The useCursor option is deprecated "
2132-
"and will be removed in PyMongo 4.0",
2133-
DeprecationWarning, stacklevel=2)
2134-
use_cursor = common.validate_boolean(
2135-
"useCursor", kwargs.pop("useCursor", True))
2136-
21372127
cmd = aggregation_command(
21382128
self, cursor_class, pipeline, kwargs, explicit_session,
2139-
user_fields={'cursor': {'firstBatch': 1}}, use_cursor=use_cursor)
2129+
user_fields={'cursor': {'firstBatch': 1}})
21402130
return self.__database.client._retryable_read(
21412131
cmd.get_cursor, cmd.get_read_preference(session), session,
21422132
retryable=not cmd._performs_write)
@@ -2155,13 +2145,10 @@ def aggregate(self, pipeline, session=None, **kwargs):
21552145
- `maxTimeMS` (int): The maximum amount of time to allow the operation
21562146
to run in milliseconds.
21572147
- `batchSize` (int): The maximum number of documents to return per
2158-
batch. Ignored if the connected mongod or mongos does not support
2159-
returning aggregate results using a cursor, or `useCursor` is
2160-
``False``.
2148+
batch.
21612149
- `collation` (optional): An instance of
21622150
:class:`~pymongo.collation.Collation`. This option is only supported
21632151
on MongoDB 3.4 and above.
2164-
- `useCursor` (bool): Deprecated. Will be removed in PyMongo 4.0.
21652152
21662153
The :meth:`aggregate` method obeys the :attr:`read_preference` of this
21672154
:class:`Collection`, except when ``$out`` or ``$merge`` are used, in
@@ -2186,6 +2173,8 @@ def aggregate(self, pipeline, session=None, **kwargs):
21862173
A :class:`~pymongo.command_cursor.CommandCursor` over the result
21872174
set.
21882175
2176+
.. versionchanged:: 4.0
2177+
Removed the ``useCursor`` option.
21892178
.. versionchanged:: 3.9
21902179
Apply this collection's read concern to pipelines containing the
21912180
`$out` stage when connected to MongoDB >= 4.2.

test/test_collection.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,12 +1615,7 @@ def test_aggregate(self):
16151615
self.assertRaises(TypeError, db.test.aggregate, "wow")
16161616

16171617
pipeline = {"$project": {"_id": False, "foo": True}}
1618-
# MongoDB 3.5.1+ requires either the 'cursor' or 'explain' options.
1619-
if client_context.version.at_least(3, 5, 1):
1620-
result = db.test.aggregate([pipeline])
1621-
else:
1622-
result = db.test.aggregate([pipeline], useCursor=False)
1623-
1618+
result = db.test.aggregate([pipeline])
16241619
self.assertTrue(isinstance(result, CommandCursor))
16251620
self.assertEqual([{'foo': [1, 2]}], list(result))
16261621

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

1658-
cursor = db.test.aggregate([projection], useCursor=True)
1659-
self.assertTrue(isinstance(cursor, CommandCursor))
1660-
16611649
def test_aggregation_cursor(self):
16621650
db = self.db
16631651
if client_context.has_secondaries:

0 commit comments

Comments
 (0)