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