Skip to content

Commit 75c0c52

Browse files
author
Dave
authored
DOCSP-22185 BACKPORT (#1112)
1 parent 3ba1ef4 commit 75c0c52

File tree

6 files changed

+123
-37
lines changed

6 files changed

+123
-37
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
@@ -259,6 +259,11 @@ have a :ref:`role <roles>` that grants the following :ref:`privilege
259259
The built-in :authrole:`read` role provides the appropriate
260260
privileges.
261261

262+
Cursor Iteration
263+
----------------
264+
265+
.. include:: /includes/fact-multiple-cursor-monitors.rst
266+
262267
Example
263268
-------
264269

@@ -272,16 +277,18 @@ except for the ``admin``, ``local``, and the ``config`` databases.
272277
watchCursor = db.getMongo().watch()
273278

274279
Iterate the cursor to check for new events. Use the
275-
:method:`cursor.isExhausted()` method to ensure the loop only exits
276-
if the change stream cursor is closed *and* there are no objects
277-
remaining in the latest batch:
280+
:method:`cursor.isClosed()` method with the :method:`cursor.tryNext()`
281+
method to ensure the loop only exits if the change stream cursor is
282+
closed *and* there are no objects remaining in the latest batch:
278283

279284
.. code-block:: javascript
280285

281-
while (!watchCursor.isExhausted()){
282-
if (watchCursor.hasNext()){
283-
printjson(watchCursor.next());
284-
}
286+
while (!watchCursor.isClosed()) {
287+
let next = watchCursor.tryNext()
288+
while (next !== null) {
289+
printjson(next);
290+
next = watchCursor.tryNext()
291+
}
285292
}
286293

287294
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
@@ -245,6 +245,11 @@ have a :ref:`role <roles>` that grants the following :ref:`privilege
245245
The built-in :authrole:`read` role provides the appropriate
246246
privileges.
247247

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

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

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

266271
.. code-block:: javascript
267272

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

274281
For complete documentation on change stream output, see
@@ -293,16 +300,18 @@ the ``data.sensors`` collection using the
293300
)
294301

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

300307
.. code-block:: javascript
301308

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

308317
For any update operation, the change event returns the result of the
@@ -334,13 +343,13 @@ filter only ``insert`` events:
334343
)
335344

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

341350
.. code-block:: javascript
342351

343-
while (!watchCursor.isExhausted()){
352+
while (!watchCursor.isClosed()){
344353
if (watchCursor.hasNext()){
345354
printjson(watchCursor.next());
346355
}
@@ -369,7 +378,7 @@ rolled off the cluster's oplog.
369378
let watchCursor = db.getSiblingDB("data").sensors.watch();
370379
let firstChange;
371380

372-
while (!watchCursor.isExhausted()) {
381+
while (!watchCursor.isClosed()) {
373382
if (watchCursor.hasNext()) {
374383
firstChange = watchCursor.next();
375384
break;
@@ -386,13 +395,14 @@ rolled off the cluster's oplog.
386395
)
387396

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

393403
.. code-block:: javascript
394404

395-
while (!resumedWatchCursor.isExhausted()){
405+
while (!resumedWatchCursor.isClosed()){
396406
if (resumedWatchCursor.hasNext()){
397407
printjson(watchCursor.next());
398408
}

source/reference/method/db.watch.txt

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

257+
Cursor Iteration
258+
----------------
259+
260+
.. include:: /includes/fact-multiple-cursor-monitors.rst
261+
257262
Example
258263
-------
259264

@@ -266,17 +271,20 @@ database.
266271

267272
watchCursor = db.getSiblingDB("hr").watch()
268273

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

274280
.. code-block:: javascript
275281

276-
while (!watchCursor.isExhausted()){
277-
if (watchCursor.hasNext()){
278-
printjson(watchCursor.next());
279-
}
282+
while (!watchCursor.isClosed()) {
283+
let next = watchCursor.tryNext()
284+
while (next !== null) {
285+
printjson(next);
286+
next = watchCursor.tryNext()
287+
}
280288
}
281289

282290
For complete documentation on change stream output, see

source/reference/method/js-cursor.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ These methods modify the way that the underlying query is executed.
121121
* - :method:`cursor.toArray()`
122122
- Returns an array that contains all documents returned by the
123123
cursor.
124+
* - :method:`cursor.tryNext()`
125+
- Returns the next element in the iteration if available or else
126+
null.
124127

125128
.. toctree::
126129
:titlesonly:
@@ -160,3 +163,4 @@ These methods modify the way that the underlying query is executed.
160163
/reference/method/cursor.sort
161164
/reference/method/cursor.tailable
162165
/reference/method/cursor.toArray
166+
/reference/method/cursor.tryNext

0 commit comments

Comments
 (0)