Challenge - 5 Problems
Change Stream Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of a simple change stream watching a database
Consider a MongoDB change stream opened on a database watching all collections. If a document is inserted into a collection, what does the change stream event's
operationType field show?MongoDB
const changeStream = db.watch(); // Insert a document into any collection // What is the value of operationType in the change event?
Attempts:
2 left
💡 Hint
Think about what operation happens when a new document is added.
✗ Incorrect
When a new document is added to any collection in the watched database, the change stream event's operationType is "insert".
🧠 Conceptual
intermediate1:30remaining
Understanding resume tokens in change streams
What is the purpose of a resume token in MongoDB change streams?
Attempts:
2 left
💡 Hint
Think about how you can continue watching changes after a disconnection.
✗ Incorrect
A resume token uniquely identifies the last event seen in a change stream, allowing the stream to resume from that point without missing events.
📝 Syntax
advanced2:30remaining
Correct syntax to watch changes on a database with a filter
Which of the following MongoDB commands correctly opens a change stream on a database filtering only insert operations?
MongoDB
const pipeline = [ { $match: { operationType: "insert" } } ]; const changeStream = db.watch(pipeline);
Attempts:
2 left
💡 Hint
Remember the aggregation pipeline syntax uses $match inside an array.
✗ Incorrect
The watch method accepts an array of aggregation pipeline stages. The $match stage filters events by operationType. Option A uses correct syntax.
🔧 Debug
advanced3:00remaining
Why does this change stream not receive any events?
A developer runs this code to watch changes on a database but never receives any events, even after inserting documents:
const changeStream = db.watch([{ $match: { operationType: "insert" } }]);
changeStream.on('change', (next) => console.log(next));
What is the most likely reason?
Attempts:
2 left
💡 Hint
Check if the watched database matches where inserts happen.
✗ Incorrect
Change streams watch a specific database or collection. If inserts happen in another database, the stream won't see them.
❓ optimization
expert3:30remaining
Optimizing change stream pipeline for minimal data transfer
You want to watch changes on a database but only need the document key (_id) and operationType fields in the change events to reduce network usage. Which pipeline optimizes the change stream to return only these fields?
Attempts:
2 left
💡 Hint
Use $project to include only needed fields and exclude the default _id.
✗ Incorrect
Option D uses $project to include only documentKey._id and operationType, excluding the default _id field from the change event, minimizing data sent.