Skip to content

Commit fab3f5e

Browse files
DOCSP-37542 Cursors (#53)
1 parent 20779fb commit fab3f5e

File tree

5 files changed

+174
-66
lines changed

5 files changed

+174
-66
lines changed

source/fundamentals/crud/cursors.txt

Lines changed: 0 additions & 65 deletions
This file was deleted.

source/includes/cursors/cursors.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# start-cursor-iterate
2+
results = collection.find()
3+
4+
for document in results:
5+
print(document)
6+
# end-cursor-iterate
7+
8+
# start-cursor-next
9+
results = collection.find({ "name": "Dunkin' Donuts" })
10+
11+
print(results.next())
12+
# end-cursor-next
13+
14+
# start-cursor-list
15+
results = collection.find({ "name": "Dunkin' Donuts" })
16+
17+
all_results = list(results)
18+
19+
for document in all_results:
20+
print(document)
21+
# end-cursor-list
22+
23+
# start-cursor-close
24+
results = collection.find()
25+
26+
...
27+
28+
results.close()
29+
# end-cursor-close
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
oplog = client.local.oplog.rs
2+
first = oplog.find().sort('$natural', pymongo.ASCENDING).limit(-1).next()
3+
print(first)
4+
ts = first['ts']
5+
6+
while True:
7+
cursor = oplog.find({'ts': {'$gt': ts}},
8+
cursor_type=pymongo.CursorType.TAILABLE_AWAIT)
9+
while cursor.alive:
10+
for doc in cursor:
11+
ts = doc['ts']
12+
print(doc)
13+
14+
# You end up here if the find() method returns no documents, or if
15+
# no new documents are added to the collection for more than 1 second.
16+
time.sleep(1)

source/read.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ Read Data from MongoDB
1212
:maxdepth: 1
1313

1414
/read/retrieve
15+
/read/cursors
1516

16-
- :ref:`pymongo-retrieve`
17+
- :ref:`pymongo-retrieve`
18+
- :ref:`pymongo-cursors`

source/read/cursors.txt

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
.. _pymongo-cursors:
2+
3+
=========================
4+
Access Data From a Cursor
5+
=========================
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 1
11+
:class: singlecol
12+
13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
17+
.. meta::
18+
:keywords: read, results, oplog
19+
20+
Overview
21+
--------
22+
23+
In this guide, you can learn how to access data from a **cursor** with
24+
{+driver-short+}.
25+
26+
A cursor is a mechanism that returns the results of a read operation in iterable
27+
batches. Because a cursor holds only a subset of documents at any given time,
28+
cursors reduce both memory consumption and network bandwidth usage.
29+
30+
Whenever {+driver-short+} performs a read operation that returns multiple
31+
documents, it automatically returns those documents in a cursor.
32+
33+
Sample Data
34+
~~~~~~~~~~~
35+
36+
The examples in this guide use the ``sample_restaurants.restaurants`` collection
37+
from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to create a
38+
free MongoDB Atlas cluster and load the sample datasets, see the :ref:`<pymongo-get-started>`.
39+
40+
.. _pymongo-cursors-iterate:
41+
42+
Access Cursor Contents Iteratively
43+
----------------------------------
44+
45+
To iterate over the contents of a cursor, use a ``for`` loop, as shown in the
46+
following example:
47+
48+
.. literalinclude:: /includes/cursors/cursors.py
49+
:start-after: start-cursor-iterate
50+
:end-before: end-cursor-iterate
51+
:language: python
52+
:copyable:
53+
54+
Retrieve Documents Individually
55+
-------------------------------
56+
57+
Retrieve documents from a cursor individually by calling the ``next()`` method.
58+
59+
The following example finds all documents in a collection with a ``name`` value
60+
of ``"Dunkin' Donuts"``. It then prints the first document in the cursor by calling the
61+
``next()`` method.
62+
63+
.. io-code-block::
64+
:copyable:
65+
66+
.. input:: /includes/cursors/cursors.py
67+
:start-after: start-cursor-next
68+
:end-before: end-cursor-next
69+
:language: python
70+
71+
.. output::
72+
73+
{'_id': ObjectId('...'), 'address': { ... }, 'borough': 'Bronx', 'cuisine': 'Donuts', 'grades': [...], 'name': "Dunkin' Donuts", 'restaurant_id': '40379573'}
74+
75+
Retrieve All Documents
76+
----------------------
77+
78+
.. warning::
79+
80+
If the number and size of documents returned by your query exceeds available
81+
application memory, your program will crash. If you expect a large result
82+
set, :ref:`access your cursor iteratively <pymongo-cursors-iterate>`.
83+
84+
To retrieve all documents from a cursor, convert the cursor into a ``list`` as
85+
shown in the following example:
86+
87+
.. literalinclude:: /includes/cursors/cursors.py
88+
:start-after: start-cursor-list
89+
:end-before: end-cursor-list
90+
:language: python
91+
:copyable:
92+
:emphasize-lines: 3
93+
94+
Close a Cursor
95+
--------------
96+
97+
By default, MongoDB closes a cursor when the client has exhausted all the
98+
results in the cursor. To explicitly close a cursor, call the ``close()`` method
99+
as shown in the following example:
100+
101+
.. literalinclude:: /includes/cursors/cursors.py
102+
:start-after: start-cursor-close
103+
:end-before: end-cursor-close
104+
:emphasize-lines: 5
105+
:language: python
106+
:copyable:
107+
108+
Tailable Cursors
109+
----------------
110+
111+
When querying on a :manual:`capped collection </core/capped-collections/>`, you
112+
can use a **tailable cursor** that remains open after the client exhausts the
113+
results in a cursor. To create a tailable cursor with capped collection,
114+
specify ``CursorType.TAILABLE_AWAIT`` in the ``cursor_type`` option of a
115+
``find()`` method.
116+
117+
The following example uses a tailable cursor to tail the oplog
118+
of a replica-set member:
119+
120+
.. literalinclude:: /includes/cursors/tailable-cursor.py
121+
:language: python
122+
:copyable:
123+
:emphasize-lines: 8
124+
125+
To learn more about tailable cursors, see the :manual:`Tailable Cursors guide
126+
</core/tailable-cursors/>` in the MongoDB Server manual.

0 commit comments

Comments
 (0)