0
0
MongoDBquery~20 mins

Watch method for real-time updates in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Real-time Watch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What does this change stream watch output?
Consider a MongoDB collection named 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));
AAn object with <code>operationType</code> as 'delete' and <code>documentKey</code> containing the _id of the deleted document.
BAn object with <code>operationType</code> as 'update' and <code>fullDocument</code> containing the inserted document.
CAn object with <code>operationType</code> as 'insert' and <code>fullDocument</code> containing the inserted document.
DNo output because watch only listens for updates, not inserts.
Attempts:
2 left
💡 Hint
Remember that the $match stage filters events by operation type.
🧠 Conceptual
intermediate
1: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?
ATo uniquely identify the change event so the stream can resume from this point if interrupted.
BTo encrypt the data sent over the change stream for security.
CTo filter change events by document fields.
DTo limit the number of change events returned in a batch.
Attempts:
2 left
💡 Hint
Think about what happens if the connection drops and you want to continue listening without missing changes.
📝 Syntax
advanced
2: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?
Adb.collection.watch([{ $match: { operationType: { $or: ['update', 'delete'] } } }])
Bdb.collection.watch([{ $match: { operationType: { $in: ['update', 'delete'] } } }])
Cdb.collection.watch([{ $match: { operationType: ['update', 'delete'] } }])
Ddb.collection.watch({ operationType: ['update', 'delete'] })
Attempts:
2 left
💡 Hint
Use MongoDB query operators to match multiple values in an array.
optimization
advanced
3: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?
AUse the watch method without a pipeline and rely on polling the collection periodically.
BWatch the entire collection without any filters and discard unwanted events in your application code.
CIncrease the batch size of the watch cursor to receive more events at once.
DUse a <code>$match</code> stage in the watch pipeline to filter only needed operation types or fields.
Attempts:
2 left
💡 Hint
Filtering early reduces the amount of data sent over the stream.
🔧 Debug
expert
3:00remaining
Why does this watch stream never receive events?
You run this code to watch a MongoDB collection for inserts:

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?
AThe MongoDB deployment is a standalone server, which does not support change streams.
BThe <code>$match</code> stage is incorrectly filtering for 'insert' instead of 'insertOne'.
CThe change stream requires a <code>fullDocument</code> option to receive events.
DThe collection is a capped collection, which does not support change streams.
Attempts:
2 left
💡 Hint
Change streams require a specific type of MongoDB deployment.