Skip to content

Commit 4957589

Browse files
committed
PYTHON-1362 - Add Collection.find_raw()
Instead of passing raw_batches=True to find(), make a separate method.
1 parent a6183a5 commit 4957589

File tree

3 files changed

+36
-36
lines changed

3 files changed

+36
-36
lines changed

Diff for: pymongo/collection.py

+22-8
Original file line numberDiff line numberDiff line change
@@ -1173,9 +1173,6 @@ def find(self, *args, **kwargs):
11731173
results to the client without waiting for the client to request
11741174
each batch, reducing latency. See notes on compatibility below.
11751175
1176-
- `raw_batches` (optional): If True, use the legacy wire protocol to
1177-
query MongoDB, and use a :class:`~pymongo.cursor.RawBSONCursor`
1178-
that returns entire batches of documents as raw BSON streams.
11791176
- `sort` (optional): a list of (key, direction) pairs
11801177
specifying the sort order for this query. See
11811178
:meth:`~pymongo.cursor.Cursor.sort` for details.
@@ -1245,7 +1242,7 @@ def find(self, *args, **kwargs):
12451242
.. versionchanged:: 3.5
12461243
Added the options `return_key`, `show_record_id`, `snapshot`,
12471244
`hint`, `max_time_ms`, `max_scan`, `min`, `max`, and `comment`.
1248-
Deprecated the option `modifiers`. Support the `raw_batches` option.
1245+
Deprecated the option `modifiers`.
12491246
12501247
.. versionchanged:: 3.4
12511248
Support the `collation` option.
@@ -1279,10 +1276,27 @@ def find(self, *args, **kwargs):
12791276
.. mongodoc:: find
12801277
12811278
"""
1282-
if kwargs.pop('raw_batches', False):
1283-
return RawBSONCursor(self, *args, **kwargs)
1284-
else:
1285-
return Cursor(self, *args, **kwargs)
1279+
return Cursor(self, *args, **kwargs)
1280+
1281+
def find_raw(self, *args, **kwargs):
1282+
"""Query the database and retrieve batches of raw BSON.
1283+
1284+
Takes the same parameters as :meth:`find` but returns a
1285+
:class:`~pymongo.cursor.RawBSONCursor`.
1286+
1287+
This example demonstrates how to work with raw batches, but in practice
1288+
raw batches should be passed to an external library that can decode
1289+
BSON into another data type, rather than used with PyMongo's
1290+
:mod:`bson` module.
1291+
1292+
>>> import bson
1293+
>>> cursor = db.test.find_raw()
1294+
>>> for batch in cursor:
1295+
... print(bson.decode_all(batch))
1296+
1297+
.. versionadded:: 3.6
1298+
"""
1299+
return RawBSONCursor(self, *args, **kwargs)
12861300

12871301
def parallel_scan(self, num_cursors, **kwargs):
12881302
"""Scan this entire collection in parallel.

Diff for: pymongo/cursor.py

+2-13
Original file line numberDiff line numberDiff line change
@@ -1235,22 +1235,11 @@ class RawBSONCursor(Cursor):
12351235
def __init__(self, *args, **kwargs):
12361236
"""Create a new cursor / iterator over raw batches of BSON data.
12371237
1238-
Should not be called directly by application developers. Pass
1239-
``raw_batches=True`` to :meth:`~pymongo.collection.Collection.find`
1238+
Should not be called directly by application developers -
1239+
see :meth:`~pymongo.collection.Collection.find_raw`
12401240
instead.
12411241
1242-
This example demonstrates how to work with raw batches, but in practice
1243-
raw batches should be passed to an external library that can decode
1244-
BSON into another data type, rather than used with PyMongo's
1245-
:mod:`bson` module.
1246-
1247-
>>> import bson
1248-
>>> cursor = db.test.find(raw_batches=True)
1249-
>>> for batch in cursor:
1250-
... print(bson.decode_all(batch))
1251-
12521242
.. mongodoc:: cursors
1253-
12541243
"""
12551244
if kwargs.get('manipulate'):
12561245
raise InvalidOperation(

Diff for: test/test_cursor.py

+12-15
Original file line numberDiff line numberDiff line change
@@ -1338,23 +1338,23 @@ def assertCursorKilled():
13381338

13391339

13401340
class TestRawBSONCursor(IntegrationTest):
1341-
def test_raw_batches(self):
1341+
def test_find_raw(self):
13421342
c = self.db.test
13431343
c.drop()
13441344
docs = [{'_id': i, 'x': 3.0 * i} for i in range(10)]
13451345
c.insert_many(docs)
1346-
batches = list(c.find(raw_batches=True).sort('_id'))
1346+
batches = list(c.find_raw().sort('_id'))
13471347
self.assertEqual(1, len(batches))
13481348
self.assertEqual(docs, decode_all(batches[0]))
13491349

13501350
def test_explain(self):
13511351
c = self.db.test
13521352
c.insert_one({})
1353-
explanation = c.find(raw_batches=True).explain()
1353+
explanation = c.find_raw().explain()
13541354
self.assertIsInstance(explanation, dict)
13551355

13561356
def test_clone(self):
1357-
cursor = self.db.test.find(raw_batches=True)
1357+
cursor = self.db.test.find_raw()
13581358
# Copy of a RawBSONCursor is also a RawBSONCursor, not a Cursor.
13591359
self.assertIsInstance(next(cursor.clone()), bytes)
13601360
self.assertIsInstance(next(copy.copy(cursor)), bytes)
@@ -1364,42 +1364,39 @@ def test_exhaust(self):
13641364
c = self.db.test
13651365
c.drop()
13661366
c.insert_many({'_id': i} for i in range(200))
1367-
result = b''.join(
1368-
c.find(raw_batches=True, cursor_type=CursorType.EXHAUST))
1369-
1367+
result = b''.join(c.find_raw(cursor_type=CursorType.EXHAUST))
13701368
self.assertEqual([{'_id': i} for i in range(200)], decode_all(result))
13711369

13721370
def test_server_error(self):
13731371
with self.assertRaises(OperationFailure) as exc:
1374-
next(self.db.test.find({'x': {'$bad': 1}}, raw_batches=True))
1372+
next(self.db.test.find_raw({'x': {'$bad': 1}}))
13751373

13761374
# The server response was decoded, not left raw.
13771375
self.assertIsInstance(exc.exception.details, dict)
13781376

13791377
def test_get_item(self):
13801378
with self.assertRaises(InvalidOperation):
1381-
self.db.test.find(raw_batches=True)[0]
1379+
self.db.test.find_raw()[0]
13821380

13831381
@client_context.require_version_min(3, 4)
13841382
def test_collation(self):
1385-
next(self.db.test.find(collation=Collation('en_US'), raw_batches=True))
1383+
next(self.db.test.find_raw(collation=Collation('en_US')))
13861384

13871385
@client_context.require_version_max(3, 2)
13881386
def test_collation_error(self):
13891387
with self.assertRaises(ConfigurationError):
1390-
next(self.db.test.find(collation=Collation('en_US'),
1391-
raw_batches=True))
1388+
next(self.db.test.find_raw(collation=Collation('en_US')))
13921389

13931390
@client_context.require_version_min(3, 2)
13941391
def test_read_concern(self):
13951392
c = self.db.get_collection("test", read_concern=ReadConcern("majority"))
1396-
next(c.find(raw_batches=True))
1393+
next(c.find_raw())
13971394

13981395
@client_context.require_version_max(3, 1)
13991396
def test_read_concern_error(self):
14001397
c = self.db.get_collection("test", read_concern=ReadConcern("majority"))
14011398
with self.assertRaises(ConfigurationError):
1402-
next(c.find(raw_batches=True))
1399+
next(c.find_raw())
14031400

14041401
def test_monitoring(self):
14051402
listener = EventListener()
@@ -1409,7 +1406,7 @@ def test_monitoring(self):
14091406
c.insert_many([{'_id': i} for i in range(10)])
14101407

14111408
listener.results.clear()
1412-
cursor = c.find(raw_batches=True, batch_size=4)
1409+
cursor = c.find_raw(batch_size=4)
14131410

14141411
# First raw batch of 4 documents.
14151412
next(cursor)

0 commit comments

Comments
 (0)