Skip to content

Commit df49819

Browse files
DOCSP-36990 cursors (#20)
1 parent 50c1245 commit df49819

File tree

1 file changed

+57
-38
lines changed

1 file changed

+57
-38
lines changed

source/fundamentals/crud/cursors.txt

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,65 @@
1-
.. uses tailable.rst
1+
.. _pymongo-cursors:
22

3+
================
34
Tailable Cursors
45
================
56

6-
By default, MongoDB will automatically close a cursor when the client has
7-
exhausted all results in the cursor. However, for `capped collections
8-
<https://mongodb.com/docs/manual/core/capped-collections/>`_ you may
9-
use a `tailable cursor
10-
<https://mongodb.com/docs/manual/core/tailable-cursors/>`_
11-
that remains open after the client exhausts the results in the initial cursor.
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: oplog
19+
20+
Overview
21+
--------
22+
23+
By default, MongoDB automatically closes a cursor when the client has
24+
exhausted all results in the cursor. However, for :manual:`capped collections </core/capped-collections/>`,
25+
you can use a :manual:`tailable cursor </core/tailable-cursors/>` that remains
26+
open after the client exhausts the results in the initial cursor.
1227

13-
The following is a basic example of using a tailable cursor to tail the oplog
14-
of a replica set member:
28+
Example
29+
-------
30+
31+
The following example uses a tailable cursor to tail the oplog
32+
of a replica-set member:
1533

1634
.. code-block:: python
1735

18-
import time
19-
20-
import pymongo
21-
22-
client = pymongo.MongoClient()
23-
oplog = client.local.oplog.rs
24-
first = oplog.find().sort('$natural', pymongo.ASCENDING).limit(-1).next()
25-
print(first)
26-
ts = first['ts']
27-
28-
while True:
29-
# For a regular capped collection CursorType.TAILABLE_AWAIT is the
30-
# only option required to create a tailable cursor. When querying the
31-
# oplog, the oplog_replay option enables an optimization to quickly
32-
# find the 'ts' value we're looking for. The oplog_replay option
33-
# can only be used when querying the oplog. Starting in MongoDB 4.4
34-
# this option is ignored by the server as queries against the oplog
35-
# are optimized automatically by the MongoDB query engine.
36-
cursor = oplog.find({'ts': {'$gt': ts}},
37-
cursor_type=pymongo.CursorType.TAILABLE_AWAIT,
38-
oplog_replay=True)
39-
while cursor.alive:
40-
for doc in cursor:
41-
ts = doc['ts']
42-
print(doc)
43-
# We end up here if the find() returned no documents or if the
44-
# tailable cursor timed out (no new documents were added to the
45-
# collection for more than 1 second).
46-
time.sleep(1)
36+
import time
37+
38+
import pymongo
39+
40+
client = pymongo.MongoClient()
41+
oplog = client.local.oplog.rs
42+
first = oplog.find().sort('$natural', pymongo.ASCENDING).limit(-1).next()
43+
print(first)
44+
ts = first['ts']
45+
46+
while True:
47+
cursor = oplog.find({'ts': {'$gt': ts}},
48+
cursor_type=pymongo.CursorType.TAILABLE_AWAIT,
49+
oplog_replay=True)
50+
while cursor.alive:
51+
for doc in cursor:
52+
ts = doc['ts']
53+
print(doc)
54+
# You end up here if the find() method returns no documents, or if the
55+
# tailable cursor times out (no new documents are added to the
56+
# collection for more than 1 second).
57+
time.sleep(1)
58+
59+
For a capped collection, ``CursorType.TAILABLE_AWAIT`` is the
60+
only option required to create a tailable cursor. When querying the
61+
oplog, the ``oplog_replay`` option enables an optimization to quickly
62+
find the ``ts`` value. You can use the ``oplog_replay`` option
63+
only when querying the oplog. Starting in MongoDB 4.4,
64+
the server ignores this option, because the MongoDB query engine automatically
65+
optimizes queries against the oplog.

0 commit comments

Comments
 (0)