0
0
MongoDBquery~20 mins

Change streams on databases in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Change Stream Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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?
A"replace"
B"insert"
C"update"
D"delete"
Attempts:
2 left
💡 Hint
Think about what operation happens when a new document is added.
🧠 Conceptual
intermediate
1:30remaining
Understanding resume tokens in change streams
What is the purpose of a resume token in MongoDB change streams?
ATo limit the number of events returned in a batch
BTo filter change events by operation type
CTo uniquely identify the last seen event so the stream can resume from there
DTo encrypt the change stream data for security
Attempts:
2 left
💡 Hint
Think about how you can continue watching changes after a disconnection.
📝 Syntax
advanced
2: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);
Aconst changeStream = db.watch([{ $match: { operationType: "insert" } }]);
Bconst changeStream = db.watch([{ $filter: { operationType: "insert" } }]);
Cconst changeStream = db.watch([{ match: { operationType: "insert" } }]);
Dconst changeStream = db.watch({ $match: { operationType: "insert" } });
Attempts:
2 left
💡 Hint
Remember the aggregation pipeline syntax uses $match inside an array.
🔧 Debug
advanced
3: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?
AThe change stream is opened on the database, but the insertions happen in a different database
BThe $match stage is incorrectly filtering out all events
CThe changeStream.on event listener is not set up correctly
DThe MongoDB server does not support change streams on databases
Attempts:
2 left
💡 Hint
Check if the watched database matches where inserts happen.
optimization
expert
3: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?
A[ { $project: { documentKey: 0, operationType: 1 } } ]
B[ { $match: { operationType: { $exists: true } } }, { $project: { _id: 0 } } ]
C[ { $project: { fullDocument: 0 } } ]
D[ { $project: { 'documentKey._id': 1, operationType: 1, _id: 0 } } ]
Attempts:
2 left
💡 Hint
Use $project to include only needed fields and exclude the default _id.