Skip to content

Instantly share code, notes, and snippets.

@shantanoo-desai
Last active October 22, 2019 16:00
Show Gist options
  • Save shantanoo-desai/44c2fc021b46d3db70ef1a92e2304917 to your computer and use it in GitHub Desktop.
Save shantanoo-desai/44c2fc021b46d3db70ef1a92e2304917 to your computer and use it in GitHub Desktop.
Change Stream for MongoDB using Windows 10, Docker, MongoDB and pymongo snippet
# Change Stream Example
# using PYMONGO v3.9.0
import sys
import pprint
from pymongo import MongoClient
# Create a Client to our Replica Set
cl = MongoClient(host='localhost', port=37017, replicaSet='rs0')
# Database: `team` # Collection: `players`
db = cl['team']
# using `watch()` for change streams in pymongo
try:
with db['players'].watch() as change_stream:
for change in change_stream:
pprint.pprint(change) ## should pretty print the newly inserted doc
except KeyboardInterrupt:
cl.close()
sys.exit()
except Exception as e:
print(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment