@@ -253,6 +253,11 @@ have a :ref:`role <roles>` that grants the following :ref:`privilege
253253The built-in :authrole:`read` role provides the appropriate
254254privileges.
255255
256+ Cursor Iteration
257+ ----------------
258+
259+ .. include:: /includes/fact-multiple-cursor-monitors.rst
260+
256261Examples
257262--------
258263
@@ -267,16 +272,18 @@ The following operation opens a change stream cursor against the
267272 watchCursor = db.getSiblingDB("data").sensors.watch()
268273
269274Iterate 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
282289For complete documentation on change stream output, see
@@ -301,16 +308,18 @@ the ``data.sensors`` collection using the
301308 )
302309
303310Iterate 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
316325For any update operation, the change event returns the result of the
@@ -538,13 +547,13 @@ filter only ``insert`` events:
538547 )
539548
540549Iterate 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
592601Iterate 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 }
0 commit comments