Challenge - 5 Problems
Real-time Watch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What does this change stream watch output?
Consider a MongoDB collection named
If a new document
orders. You run this watch method to listen for insertions:const changeStream = db.orders.watch([{ $match: { operationType: 'insert' } }]);
changeStream.on('change', change => console.log(change));If a new document
{ _id: 1, item: 'book', qty: 3 } is inserted, what will the change object contain?MongoDB
const changeStream = db.orders.watch([{ $match: { operationType: 'insert' } }]); changeStream.on('change', change => console.log(change));
Attempts:
2 left
💡 Hint
Remember that the
$match stage filters events by operation type.✗ Incorrect
The watch method listens for changes matching the filter. Since the filter is for 'insert', the change event will have
operationType 'insert' and include the inserted document in fullDocument.🧠 Conceptual
intermediate1:30remaining
What is the purpose of the
resumeToken in MongoDB watch streams?When using the watch method for real-time updates, MongoDB provides a
resumeToken with each change event. What is its main purpose?Attempts:
2 left
💡 Hint
Think about what happens if the connection drops and you want to continue listening without missing changes.
✗ Incorrect
The
resumeToken marks the last seen event so the watch stream can restart from that point, avoiding missed or duplicated events.📝 Syntax
advanced2:30remaining
Which watch method syntax correctly listens for updates and deletes only?
You want to listen to a MongoDB collection for only
update and delete operations using the watch method. Which of the following code snippets is correct?Attempts:
2 left
💡 Hint
Use MongoDB query operators to match multiple values in an array.
✗ Incorrect
The
$in operator matches any value in the specified array. The other options use invalid syntax for matching multiple values.❓ optimization
advanced3:00remaining
How to reduce resource usage when watching a large collection?
You have a large MongoDB collection with frequent changes. Watching all changes causes high resource use. Which approach best reduces resource usage while still capturing relevant changes?
Attempts:
2 left
💡 Hint
Filtering early reduces the amount of data sent over the stream.
✗ Incorrect
Applying a $match filter in the watch pipeline limits events sent to the client, reducing network and processing load.
🔧 Debug
expert3:00remaining
Why does this watch stream never receive events?
You run this code to watch a MongoDB collection for inserts:
But no events are logged even after inserting documents. What is the most likely cause?
const changeStream = db.collection.watch([{ $match: { operationType: 'insert' } }]);
changeStream.on('change', change => console.log(change));But no events are logged even after inserting documents. What is the most likely cause?
Attempts:
2 left
💡 Hint
Change streams require a specific type of MongoDB deployment.
✗ Incorrect
Change streams only work on replica sets or sharded clusters. Standalone servers do not support them, so no events are received.