Change stream is shut down after running updates_every_one_second.ipynb

From change_streams.ipynb

(open the change stream)
(go to `updates_every_one_second` and start the third cell)

The problem is the 3rd cell begins with inventory.drop(), which stops the watch in change_streams.ipynb. Maybe the instructions can be clearer, or the notebook edited to give learners an easier time.

I can intuitively understand why the watch will break when the collection being watched is dropped, but why isn’t any exception catching it?
This block below is from change_streams.ipynb

except pymongo.errors.PyMongoError:
    logging.error('Change stream closed because of an error.')

I looked at Jupyter output in vscode and see nothing. To make sure it’s really not logging, i changed logging to print and still see nothing in the notebook when i drop the collection to break the watch. I also added the most general except Exception but still not exception is caught.
Is it expected that dropping a collection should stop the watch? Are there any exceptions thrown when this happens or mongo is handling such things at the database level and nothing is raised so the exception catching code in application doesn’t see anything?

Hey @Han_N_A,

Watch notifies of data changes, when a collection is dropped, it is also a change in data, hence watch reports it as well. The code goes into an exception when the try block fails ie. if the ChangeStream encountered an unrecoverable error, which is not the case here. The output we get after running inventory.drop() in the change_streams.ipynb is:

{'_id': {'_data': '8262C68B1B000000092B022C0100296E5A100470058FD4DD4C4CFC8130486EC003AB1404'}, 'operationType': 'drop', 'clusterTime': Timestamp(1657178907, 9), 'ns': {'db': 'lessons', 'coll': 'inventory'}}

As you can see from the operationType, the watch is notifying us about the drop in the inventory collection. You can read more about the change streams and .watch from the documentation that I’m linking below:
Change Streams

Please feel free to reach out for anything else as well.


Thanks for the sample output.
I realized I wasn’t seeing anything because the 2nd example in change_streams.ipynb had a pipeline that filtered out output.
I tried the drop while running the 1st example and saw

{'_id': {'_data': '8262C7CD77000000012B022C0100296E5A100479EC9BEA99604D0EAD80468F103856F804'}, 'operationType': 'drop', 'clusterTime': Timestamp(1657261431, 1), 'ns': {'db': 'lessons', 'coll': 'inventory'}}
{'_id': {'_data': '8262C7CD77000000012B022C0100296F5A100479EC9BEA99604D0EAD80468F103856F804'}, 'operationType': 'invalidate', 'clusterTime': Timestamp(1657261431, 1)}

so it makes sense that there’s no error raised and nothing to catch here.

I also found from explaining why the cell stopped running (invalidate events close the change stream cursor.)

1 Like

It seems I had completely ignored the notebooks folder when I took the course :wink: your post came as a reminder of that

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.