Created
March 17, 2026 15:36
-
-
Save cheeyeo/ef4cedee1ad3d555740d6e3957f73786 to your computer and use it in GitHub Desktop.
MongoDB Change stream example using pyMongo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from server.config import get_settings | |
| from pymongo import MongoClient | |
| DATABASE_URL = f"mongodb://{get_settings().mongo_db_root_username}:{get_settings().mongo_db_root_password}@mongo1:27017,mongo2:27017,mongo3:27017/" | |
| client = MongoClient(DATABASE_URL) | |
| db = client.get_database("teststreams") | |
| users = db.create_collection("users", changeStreamPreAndPostImages={"enabled": True}) | |
| pipeline = [ | |
| { | |
| "$match": { | |
| "operationType": {"$in": ["insert", "update", "replace", "delete"]} | |
| } | |
| } | |
| ] | |
| with users.watch(pipeline, full_document="updateLookup", full_document_before_change="required") as stream: | |
| for change in stream: | |
| print(change) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment