Skip to content

Commit 4650f92

Browse files
author
Dave
authored
DOCSP-22185 BACKPORT v5.0 (#1113)
* DOCSP-22185 BACKPORT * Staging fixes * Staging fixes
1 parent 68604e7 commit 4650f92

File tree

6 files changed

+125
-39
lines changed

6 files changed

+125
-39
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
MongoDB provides multiple ways to iterate on a cursor.
2+
3+
The :method:`cursor.hasNext()` method blocks and waits for the next
4+
event. To monitor the ``watchCursor`` cursor and iterate over the
5+
events, use ``hasNext()`` like this:
6+
7+
.. code-block:: javascript
8+
9+
while (!watchCursor.isClosed()) {
10+
if (watchCursor.hasNext()) {
11+
firstChange = watchCursor.next();
12+
break;
13+
}
14+
}
15+
16+
The :method:`cursor.tryNext()` method is non-blocking. To monitor
17+
the ``watchCursor`` cursor and iterate over the events, use
18+
``tryNext()`` like this:
19+
20+
.. code-block:: javascript
21+
22+
while (!watchCursor.isClosed()) {
23+
let next = watchCursor.tryNext()
24+
while (next !== null) {
25+
printjson(next);
26+
next = watchCursor.tryNext()
27+
}
28+
}

source/reference/method/Mongo.watch.txt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,11 @@ have a :ref:`role <roles>` that grants the following :ref:`privilege
251251
The built-in :authrole:`read` role provides the appropriate
252252
privileges.
253253

254+
Cursor Iteration
255+
----------------
256+
257+
.. include:: /includes/fact-multiple-cursor-monitors.rst
258+
254259
Example
255260
-------
256261

@@ -264,16 +269,18 @@ except for the ``admin``, ``local``, and the ``config`` databases.
264269
watchCursor = db.getMongo().watch()
265270

266271
Iterate the cursor to check for new events. Use the
267-
:method:`cursor.isExhausted()` method to ensure the loop only exits
268-
if the change stream cursor is closed *and* there are no objects
269-
remaining in the latest batch:
272+
:method:`cursor.isClosed()` method with the :method:`cursor.tryNext()`
273+
method to ensure the loop only exits if the change stream cursor is
274+
closed *and* there are no objects remaining in the latest batch:
270275

271276
.. code-block:: javascript
272277

273-
while (!watchCursor.isExhausted()){
274-
if (watchCursor.hasNext()){
275-
printjson(watchCursor.next());
276-
}
278+
while (!watchCursor.isClosed()) {
279+
let next = watchCursor.tryNext()
280+
while (next !== null) {
281+
printjson(next);
282+
next = watchCursor.tryNext()
283+
}
277284
}
278285

279286
For complete documentation on change stream output, see
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=============
2+
cursor.next()
3+
=============
4+
5+
.. default-domain:: mongodb
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 1
11+
:class: singlecol
12+
13+
.. method:: cursor.tryNext()
14+
15+
16+
.. include:: /includes/fact-mongosh-shell-method.rst
17+
18+
19+
:returns: The next document in the cursor returned by the
20+
:method:`db.collection.find()` method or ``null``.
21+
22+
23+
Behavior
24+
--------
25+
26+
``cursor.tryNext()`` is a special case of the :method:`cursor.next()`
27+
method that returns the next element in the iteration if available or
28+
else ``null``.
29+

source/reference/method/db.collection.watch.txt

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,11 @@ have a :ref:`role <roles>` that grants the following :ref:`privilege
246246
The built-in :authrole:`read` role provides the appropriate
247247
privileges.
248248

249+
Cursor Iteration
250+
----------------
251+
252+
.. include:: /includes/fact-multiple-cursor-monitors.rst
253+
249254
Examples
250255
--------
251256

@@ -260,16 +265,18 @@ The following operation opens a change stream cursor against the
260265
watchCursor = db.getSiblingDB("data").sensors.watch()
261266

262267
Iterate the cursor to check for new events. Use the
263-
:method:`cursor.isExhausted()` method to ensure the loop only exits
264-
if the change stream cursor is closed *and* there are no objects
265-
remaining in the latest batch:
268+
:method:`cursor.isClosed()` method with the :method:`cursor.tryNext()`
269+
method to ensure the loop only exits if the change stream cursor is
270+
closed *and* there are no objects remaining in the latest batch:
266271

267272
.. code-block:: javascript
268273

269-
while (!watchCursor.isExhausted()){
270-
if (watchCursor.hasNext()){
271-
printjson(watchCursor.next());
272-
}
274+
while (!watchCursor.isClosed()) {
275+
let next = watchCursor.tryNext()
276+
while (next !== null) {
277+
printjson(next);
278+
next = watchCursor.tryNext()
279+
}
273280
}
274281

275282
For complete documentation on change stream output, see
@@ -294,16 +301,18 @@ the ``data.sensors`` collection using the
294301
)
295302

296303
Iterate the cursor to check for new events. Use the
297-
:method:`cursor.isExhausted()` method to ensure the loop only exits
298-
if the change stream cursor is closed *and* there are no objects
299-
remaining in the latest batch:
304+
:method:`cursor.isClosed()` method with the :method:`cursor.tryNext()`
305+
method to ensure the loop only exits if the change stream cursor is
306+
closed *and* there are no objects remaining in the latest batch:
300307

301308
.. code-block:: javascript
302309

303-
while (!watchCursor.isExhausted()){
304-
if (watchCursor.hasNext()){
305-
printjson(watchCursor.next());
306-
}
310+
while (!watchCursor.isClosed()) {
311+
let next = watchCursor.tryNext()
312+
while (next !== null) {
313+
printjson(next);
314+
next = watchCursor.tryNext()
315+
}
307316
}
308317

309318
For any update operation, the change event returns the result of the
@@ -335,13 +344,13 @@ filter only ``insert`` events:
335344
)
336345

337346
Iterate the cursor to check for new events. Use the
338-
:method:`cursor.isExhausted()` method to ensure the loop only exits
339-
if the change stream cursor is closed *and* there are no objects
340-
remaining in the latest batch:
347+
:method:`cursor.isClosed()` method with the :method:`cursor.hasNext()`
348+
method to ensure the loop only exits if the change stream cursor is
349+
closed *and* there are no objects remaining in the latest batch:
341350

342351
.. code-block:: javascript
343352

344-
while (!watchCursor.isExhausted()){
353+
while (!watchCursor.isClosed()){
345354
if (watchCursor.hasNext()){
346355
printjson(watchCursor.next());
347356
}
@@ -370,7 +379,7 @@ rolled off the cluster's oplog.
370379
let watchCursor = db.getSiblingDB("data").sensors.watch();
371380
let firstChange;
372381

373-
while (!watchCursor.isExhausted()) {
382+
while (!watchCursor.isClosed()) {
374383
if (watchCursor.hasNext()) {
375384
firstChange = watchCursor.next();
376385
break;
@@ -387,13 +396,14 @@ rolled off the cluster's oplog.
387396
)
388397

389398
Iterate the cursor to check for new events. Use the
390-
:method:`cursor.isExhausted()` method to ensure the loop only exits
391-
if the change stream cursor is closed *and* there are no objects
392-
remaining in the latest batch:
399+
:method:`cursor.isClosed()` method with the :method:`cursor.hasNext()`
400+
method to ensure the loop only exits if the change stream cursor is
401+
closed *and* there are no objects remaining in the latest batch:
402+
393403

394404
.. code-block:: javascript
395405

396-
while (!resumedWatchCursor.isExhausted()){
406+
while (!resumedWatchCursor.isClosed()){
397407
if (resumedWatchCursor.hasNext()){
398408
printjson(watchCursor.next());
399409
}

source/reference/method/db.watch.txt

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,11 @@ have a :ref:`role <roles>` that grants the following :ref:`privilege
250250
The built-in :authrole:`read` role provides the appropriate
251251
privileges.
252252

253+
Cursor Iteration
254+
----------------
255+
256+
.. include:: /includes/fact-multiple-cursor-monitors.rst
257+
253258
Example
254259
-------
255260

@@ -262,17 +267,20 @@ database.
262267

263268
watchCursor = db.getSiblingDB("hr").watch()
264269

270+
265271
Iterate the cursor to check for new events. Use the
266-
:method:`cursor.isExhausted()` method to ensure the loop only exits
267-
if the change stream cursor is closed *and* there are no objects
268-
remaining in the latest batch:
272+
:method:`cursor.isClosed()` method with the :method:`cursor.tryNext()`
273+
method to ensure the loop only exits if the change stream cursor is
274+
closed *and* there are no objects remaining in the latest batch:
269275

270276
.. code-block:: javascript
271277

272-
while (!watchCursor.isExhausted()){
273-
if (watchCursor.hasNext()){
274-
printjson(watchCursor.next());
275-
}
278+
while (!watchCursor.isClosed()) {
279+
let next = watchCursor.tryNext()
280+
while (next !== null) {
281+
printjson(next);
282+
next = watchCursor.tryNext()
283+
}
276284
}
277285

278286
For complete documentation on change stream output, see

source/reference/method/js-cursor.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,12 @@ These methods modify the way that the underlying query is executed.
157157
- Marks the cursor as tailable. Only valid for cursors over capped collections.
158158

159159
* - :method:`cursor.toArray()`
160+
- Returns an array that contains all documents returned by the
161+
cursor.
160162

161-
- Returns an array that contains all documents returned by the cursor.
162-
163+
* - :method:`cursor.tryNext()`
164+
- Returns the next element in the iteration if available or else
165+
null.
163166

164167
.. toctree::
165168
:titlesonly:
@@ -198,3 +201,4 @@ These methods modify the way that the underlying query is executed.
198201
/reference/method/cursor.sort
199202
/reference/method/cursor.tailable
200203
/reference/method/cursor.toArray
204+
/reference/method/cursor.tryNext

0 commit comments

Comments
 (0)