Monitor Database Changes with MongoDB Change Streams
📖 Scenario: You are managing a MongoDB database for an online bookstore. You want to track any changes made to the books collection in real-time to update your inventory system promptly.
🎯 Goal: Build a MongoDB change stream that listens to changes on the books collection and captures insert, update, and delete events.
📋 What You'll Learn
Create a MongoDB collection named books with sample documents.
Define a change stream on the books collection.
Configure the change stream to listen for insert, update, and delete operations.
Implement the change stream to print the change events.
💡 Why This Matters
🌍 Real World
Change streams help applications react instantly to database changes, such as updating user interfaces or syncing data across services.
💼 Career
Understanding change streams is valuable for backend developers and database administrators who build real-time data-driven applications.
Progress0 / 4 steps
1
Create the books collection with sample data
Create a MongoDB collection called books and insert these exact documents: { _id: 1, title: "The Hobbit", author: "J.R.R. Tolkien" } and { _id: 2, title: "1984", author: "George Orwell" }.
MongoDB
Hint
Use db.books.insertMany() with an array of documents.
2
Define a change stream cursor on the books collection
Create a variable called changeStream that opens a change stream on the books collection using db.books.watch().
MongoDB
Hint
Use db.books.watch() to create the change stream cursor.
3
Configure the change stream to filter for insert, update, and delete events
Modify the changeStream to watch only for insert, update, and delete operation types by passing a pipeline with $match to db.books.watch().
MongoDB
Hint
Pass a pipeline array with { $match: { operationType: { $in: [...] } } } to db.books.watch().
4
Listen for change events and print them
Use changeStream.on('change', callback) to listen for change events and print each event using console.log(change) inside the callback function.
MongoDB
Hint
Use changeStream.on('change', (change) => { console.log(change) }) to listen and print events.
Practice
(1/5)
1. What is the main purpose of MongoDB change streams on databases?
easy
A. To listen for real-time changes in the database
B. To backup the database automatically
C. To create indexes on collections
D. To run queries faster
Solution
Step 1: Understand what change streams do
Change streams allow applications to watch for changes like inserts, updates, or deletes in real-time.
Step 2: Compare options with this purpose
Only To listen for real-time changes in the database describes listening to real-time changes, which matches the main purpose of change streams.
Final Answer:
To listen for real-time changes in the database -> Option A
A. The watch() method cannot be called on a database object
B. The database should be accessed using getSiblingDB('sales') before calling watch()
C. The on() method is not valid for change streams
D. Missing parentheses after watch
Solution
Step 1: Check how the database is accessed
Using db.sales accesses a collection named 'sales' in the current database, not the database itself.
Step 2: Correct way to access a different database
To watch changes on the 'sales' database, use db.getSiblingDB('sales').watch().
Final Answer:
The database should be accessed using getSiblingDB('sales') before calling watch() -> Option B
Quick Check:
Use getSiblingDB to access database, not db.sales [OK]
Hint: Use getSiblingDB('name') to access database, not db.name [OK]
Common Mistakes:
Assuming db.sales is a database instead of a collection
Thinking watch() can't be called on database objects
Confusing event handling methods
5. You want to watch only update operations on the orders database using change streams. Which code snippet correctly applies a filter to achieve this?
hard
A. db.getSiblingDB('orders').watch([{ $match: { operationType: 'update' } }])
B. db.orders.watch({ operationType: 'update' })
C. db.getSiblingDB('orders').watch({ filter: { operationType: 'update' } })
D. db.getSiblingDB('orders').watch([{ $filter: { operationType: 'update' } }])
Solution
Step 1: Recall how to filter change streams
Change streams accept an aggregation pipeline array to filter events, using stages like $match.
Step 2: Apply correct syntax for filtering updates
The correct filter is [{ $match: { operationType: 'update' } }] passed as an array to watch().
Final Answer:
db.getSiblingDB('orders').watch([{ $match: { operationType: 'update' } }]) -> Option A
Quick Check:
Use $match in array to filter change streams [OK]
Hint: Use watch([{ $match: { operationType: 'update' } }]) to filter updates [OK]
Common Mistakes:
Passing filter as object instead of pipeline array
Using invalid keys like 'filter' or '$filter'
Calling watch() on collection instead of database
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.