- Reference >
mongo
Shell Methods >- Cursor Methods >
- cursor.isExhausted()
cursor.isExhausted()¶
On this page
-
cursor.
isExhausted
()¶ mongo
Shell MethodThis page documents the
mongo
shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.Returns: Boolean. cursor.isExhausted()
returnstrue
if the cursor is closed and there are no remaining objects in the batch.Use
isExhausted()
to support iterating cursors that remain open even if there are no documents remaining in the current batch, such astailable
orchange stream
cursors.
Example¶
Consider the following while
loop iterating through updates to
a change stream
cursor:
A change stream cursor can return an empty batch if no new data changes
have occured within a set period of time. This causes the while loop
to exit prematurely as cursor.hasNext()
returns false
when it detects the empty batch. However, the change stream cursor
is still open and able to return more documents in the future.
Use cursor.isExhausted()
to ensure the while loop only exits
when the cursor is closed and there are no documents remaining in the
batch: