Docs Menu

Docs HomeMongoDB Manual


On this page

  • Example


mongosh Method

This is a mongosh method. This is not the documentation for Node.js or other programming language specific driver methods.

In most cases, mongosh methods work the same way as the legacy mongo shell methods. However, some legacy methods are unavailable in mongosh.

For the legacy mongo shell documentation, refer to the documentation for the corresponding MongoDB Server release:

For MongoDB API drivers, refer to the language specific MongoDB driver documentation.


cursor.isExhausted() returns true 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 as tailable or change stream cursors.

Consider the following while loop iterating through updates to a change stream cursor:

watchCursor =;
while (watchCursor.hasNext()) {;

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:

watchCursor =;
while (!watchCursor.isExhausted()) {
if (watchCursor.hasNext()){;
←  cursor.hint()cursor.itcount() →

On this page

Share Feedback
© 2023 MongoDB, Inc.


  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2023 MongoDB, Inc.