@@ -245,6 +245,11 @@ have a :ref:`role <roles>` that grants the following :ref:`privilege
245
245
The built-in :authrole:`read` role provides the appropriate
246
246
privileges.
247
247
248
+ Cursor Iteration
249
+ ----------------
250
+
251
+ .. include:: /includes/fact-multiple-cursor-monitors.rst
252
+
248
253
Examples
249
254
--------
250
255
@@ -259,16 +264,18 @@ The following operation opens a change stream cursor against the
259
264
watchCursor = db.getSiblingDB("data").sensors.watch()
260
265
261
266
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:
265
270
266
271
.. code-block:: javascript
267
272
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
+ }
272
279
}
273
280
274
281
For complete documentation on change stream output, see
@@ -293,16 +300,18 @@ the ``data.sensors`` collection using the
293
300
)
294
301
295
302
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:
299
306
300
307
.. code-block:: javascript
301
308
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
+ }
306
315
}
307
316
308
317
For any update operation, the change event returns the result of the
@@ -334,13 +343,13 @@ filter only ``insert`` events:
334
343
)
335
344
336
345
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:
340
349
341
350
.. code-block:: javascript
342
351
343
- while (!watchCursor.isExhausted ()){
352
+ while (!watchCursor.isClosed ()){
344
353
if (watchCursor.hasNext()){
345
354
printjson(watchCursor.next());
346
355
}
@@ -369,7 +378,7 @@ rolled off the cluster's oplog.
369
378
let watchCursor = db.getSiblingDB("data").sensors.watch();
370
379
let firstChange;
371
380
372
- while (!watchCursor.isExhausted ()) {
381
+ while (!watchCursor.isClosed ()) {
373
382
if (watchCursor.hasNext()) {
374
383
firstChange = watchCursor.next();
375
384
break;
@@ -386,13 +395,14 @@ rolled off the cluster's oplog.
386
395
)
387
396
388
397
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
+
392
402
393
403
.. code-block:: javascript
394
404
395
- while (!resumedWatchCursor.isExhausted ()){
405
+ while (!resumedWatchCursor.isClosed ()){
396
406
if (resumedWatchCursor.hasNext()){
397
407
printjson(watchCursor.next());
398
408
}
0 commit comments