Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-15240

[Server] Open change streams iteration example script returns error from mongosh session

      Summary: 

      I found the example scripts listed in the below article to open a change stream are not compatible in a session using the mongosh binary.

      https://www.mongodb.com/docs/v5.0/reference/method/db.collection.watch/#examples

      The below error occurs when attempting to execute the cursor.IsExhausted() method on a change stream cursor opened within mongosh.

      while (!watchCursor.isExhausted()){   if (watchCursor.hasNext()){      printjson(watchCursor.next());   }} 
      MongoshInvalidInputError: isExhausted is not implemented for ChangeStreams because after closing a cursor, the remaining documents in the batch are no longer accessible. If you want to see if the cursor is closed use isClosed. If you want to see if there are documents left in the batch, use tryNext.

      This script does work fine with the legacy mongo binary.

      As a workaround with mongosh, I used the isClosed() method instead in the while loop like below:

      while (!watchCursor.isClosed()) {  if (watchCursor.hasNext()){           printjson(watchCursor.next());    }}
      

      Wondering here if the docs should be updated to note incompatibility of cursor.isExhausted() against a change stream cursor from mongosh, and/or suggest an alternate example of iterating the change stream cursor with mongosh.

       

       

            Assignee:
            lauren.tran@mongodb.com Lauren Tran
            Reporter:
            shane.smith@mongodb.com Shane Smith
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved:
              33 weeks, 4 days ago