Skip to content

Commit 30de616

Browse files
author
Dave
authored
DOCSP-22185 watch gives bad advice v6.0 (#1097)
* DOCSP-22185 watch give bad advice * DOCSP-22185 watch give bad advice * DOCSP-22185 watch gives bad advice * DOCSP-22185 watch gives bad advice * Review Feedback
1 parent d123899 commit 30de616

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
@@ -253,6 +253,11 @@ have a :ref:`role <roles>` that grants the following :ref:`privilege
253253
The built-in :authrole:`read` role provides the appropriate
254254
privileges.
255255

256+
Cursor Iteration
257+
----------------
258+
259+
.. include:: /includes/fact-multiple-cursor-monitors.rst
260+
256261
Examples
257262
--------
258263

@@ -267,16 +272,18 @@ The following operation opens a change stream cursor against the
267272
watchCursor = db.getSiblingDB("data").sensors.watch()
268273

269274
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:
275+
:method:`cursor.isClosed()` method with the :method:`cursor.tryNext()`
276+
method to ensure the loop only exits if the change stream cursor is
277+
closed *and* there are no objects remaining in the latest batch:
273278

274279
.. code-block:: javascript
275280

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

282289
For complete documentation on change stream output, see
@@ -301,16 +308,18 @@ the ``data.sensors`` collection using the
301308
)
302309

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

308315
.. code-block:: javascript
309316

310-
while (!watchCursor.isExhausted()){
311-
if (watchCursor.hasNext()){
312-
printjson(watchCursor.next());
313-
}
317+
while (!watchCursor.isClosed()) {
318+
let next = watchCursor.tryNext()
319+
while (next !== null) {
320+
printjson(next);
321+
next = watchCursor.tryNext()
322+
}
314323
}
315324

316325
For any update operation, the change event returns the result of the
@@ -538,13 +547,13 @@ filter only ``insert`` events:
538547
)
539548

540549
Iterate the cursor to check for new events. Use the
541-
:method:`cursor.isExhausted()` method to ensure the loop only exits
542-
if the change stream cursor is closed *and* there are no objects
543-
remaining in the latest batch:
550+
:method:`cursor.isClosed()` method with the :method:`cursor.hasNext()`
551+
method to ensure the loop only exits if the change stream cursor is
552+
closed *and* there are no objects remaining in the latest batch:
544553

545554
.. code-block:: javascript
546555

547-
while (!watchCursor.isExhausted()){
556+
while (!watchCursor.isClosed()){
548557
if (watchCursor.hasNext()){
549558
printjson(watchCursor.next());
550559
}
@@ -573,7 +582,7 @@ rolled off the cluster's oplog.
573582
let watchCursor = db.getSiblingDB("data").sensors.watch();
574583
let firstChange;
575584

576-
while (!watchCursor.isExhausted()) {
585+
while (!watchCursor.isClosed()) {
577586
if (watchCursor.hasNext()) {
578587
firstChange = watchCursor.next();
579588
break;
@@ -590,13 +599,14 @@ rolled off the cluster's oplog.
590599
)
591600

592601
Iterate the cursor to check for new events. Use the
593-
:method:`cursor.isExhausted()` method to ensure the loop only exits
594-
if the change stream cursor is closed *and* there are no objects
595-
remaining in the latest batch:
602+
:method:`cursor.isClosed()` method with the :method:`cursor.hasNext()`
603+
method to ensure the loop only exits if the change stream cursor is
604+
closed *and* there are no objects remaining in the latest batch:
605+
596606

597607
.. code-block:: javascript
598608

599-
while (!resumedWatchCursor.isExhausted()){
609+
while (!resumedWatchCursor.isClosed()){
600610
if (resumedWatchCursor.hasNext()){
601611
printjson(watchCursor.next());
602612
}

source/reference/method/db.watch.txt

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

265+
Cursor Iteration
266+
----------------
267+
268+
.. include:: /includes/fact-multiple-cursor-monitors.rst
269+
265270
Example
266271
-------
267272

@@ -274,17 +279,20 @@ database.
274279

275280
watchCursor = db.getSiblingDB("hr").watch()
276281

282+
277283
Iterate the cursor to check for new events. Use the
278-
:method:`cursor.isExhausted()` method to ensure the loop only exits
279-
if the change stream cursor is closed *and* there are no objects
280-
remaining in the latest batch:
284+
:method:`cursor.isClosed()` method with the :method:`cursor.tryNext()`
285+
method to ensure the loop only exits if the change stream cursor is
286+
closed *and* there are no objects remaining in the latest batch:
281287

282288
.. code-block:: javascript
283289

284-
while (!watchCursor.isExhausted()){
285-
if (watchCursor.hasNext()){
286-
printjson(watchCursor.next());
287-
}
290+
while (!watchCursor.isClosed()) {
291+
let next = watchCursor.tryNext()
292+
while (next !== null) {
293+
printjson(next);
294+
next = watchCursor.tryNext()
295+
}
288296
}
289297

290298
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)