-
Type: Task
-
Resolution: Won't Do
-
Priority: Minor - P4
-
Affects Version/s: 5.0.6
-
Labels:
-
Environment:Sample Environment:
Mongodb Server 5.0.6
mongodb-mongosh-1.2.2-1.el7.x86_64
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.