Change streams on databases in MongoDB - Time & Space Complexity
When using change streams in MongoDB, it's important to understand how the time to process changes grows as more changes happen.
We want to know how the cost of watching and reacting to database changes scales with the number of changes.
Analyze the time complexity of the following code snippet.
const changeStream = db.collection('orders').watch();
changeStream.on('change', (change) => {
// Process each change event
console.log(change);
});
This code listens for changes on the 'orders' collection and processes each change as it arrives.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Processing each change event as it arrives.
- How many times: Once per change event, repeating indefinitely as changes occur.
Each new change event triggers one processing operation.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 processing calls |
| 100 | 100 processing calls |
| 1000 | 1000 processing calls |
Pattern observation: The number of operations grows directly with the number of changes.
Time Complexity: O(n)
This means the time to process changes grows linearly with the number of change events.
[X] Wrong: "Change streams process all changes instantly regardless of how many there are."
[OK] Correct: Each change event requires processing time, so more changes mean more work and longer total processing time.
Understanding how change streams scale helps you explain real-time data handling and event-driven designs clearly and confidently.
"What if we added filtering to the change stream to only process certain changes? How would the time complexity change?"
Practice
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 AQuick Check:
Change streams = real-time listening [OK]
- Confusing change streams with backups
- Thinking change streams create indexes
- Assuming change streams speed up queries
mydb?Solution
Step 1: Recall how to access a database in MongoDB shell
To access a database other than the current one, usedb.getSiblingDB('mydb').Step 2: Apply the watch() method on the database object
Thewatch()method is called on the database object to start change streams.Final Answer:
db.getSiblingDB('mydb').watch() -> Option DQuick Check:
Use getSiblingDB to access db, then watch() [OK]
- Trying to call watch() directly on db with database name
- Using incorrect method like db.watch('name')
- Confusing method order or syntax
testdb database?
const changeStream = db.getSiblingDB('testdb').watch();
changeStream.forEach((change) => {
printjson(change.operationType);
});Solution
Step 1: Understand what triggers the change event
The change stream listens to all changes in the database, including inserts, updates, and deletes.Step 2: Identify the operationType for an insert
When a document is inserted, theoperationTypeis "insert".Final Answer:
"insert" -> Option CQuick Check:
Insert triggers operationType = "insert" [OK]
- Expecting 'update' or 'delete' for insert events
- Thinking no output occurs without filters
- Confusing event names with method names
sales database:
const changeStream = db.sales.watch();
changeStream.forEach((change) => {
printjson(change);
});Solution
Step 1: Check how the database is accessed
Usingdb.salesaccesses 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, usedb.getSiblingDB('sales').watch().Final Answer:
The database should be accessed using getSiblingDB('sales') before calling watch() -> Option BQuick Check:
Use getSiblingDB to access database, not db.sales [OK]
- Assuming db.sales is a database instead of a collection
- Thinking watch() can't be called on database objects
- Confusing event handling methods
orders database using change streams. Which code snippet correctly applies a filter to achieve this?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 towatch().Final Answer:
db.getSiblingDB('orders').watch([{ $match: { operationType: 'update' } }]) -> Option AQuick Check:
Use $match in array to filter change streams [OK]
- Passing filter as object instead of pipeline array
- Using invalid keys like 'filter' or '$filter'
- Calling watch() on collection instead of database
Ready to prove it?
Pass the test and this pattern moves unknown
