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 inserts
Consider a MongoDB collection users. You open a change stream with the pipeline filtering only
insert operations. If a new document {_id: 1, name: 'Alice'} is inserted, what will the change stream output contain?MongoDB
const changeStream = db.users.watch([{ $match: { operationType: 'insert' } }]); // After inserting { _id: 1, name: 'Alice' }
Attempts:
2 left
💡 Hint
Change streams capture insert operations and include the full inserted document.
✗ Incorrect
Change streams emit events for operations like insert, update, and delete. When filtering for inserts, the event includes the inserted document in fullDocument.
❓ query_result
intermediate2:00remaining
Filtering change stream events by field value
You want to watch changes on the orders collection but only for documents where the
status field is 'shipped'. Which pipeline will correctly filter these events?MongoDB
const pipeline = [ { $match: { 'fullDocument.status': 'shipped' } } ]; const changeStream = db.orders.watch(pipeline);
Attempts:
2 left
💡 Hint
The fullDocument field contains the entire changed document after the operation.
✗ Incorrect
To filter by a field value in the changed document, you match on 'fullDocument.fieldName'.
❓ schema
advanced2:00remaining
Required collection configuration for change streams
Which of the following is required for a MongoDB collection to support change streams?
Attempts:
2 left
💡 Hint
Change streams rely on the oplog which exists only in replica sets or sharded clusters.
✗ Incorrect
Change streams require the collection to be in a replica set or sharded cluster because they use the oplog to track changes.
🔧 Debug
advanced2:00remaining
Why does this change stream not receive any events?
You run this code to watch changes on the
products collection:
const changeStream = db.products.watch([{ $match: { operationType: 'insert' } }]);
changeStream.on('change', (next) => console.log(next));
But no events appear even after inserting documents. What is the most likely reason?Attempts:
2 left
💡 Hint
Change streams require a replica set or sharded cluster to work.
✗ Incorrect
Change streams do not work on standalone MongoDB servers because they rely on the oplog available only in replica sets or sharded clusters.
❓ optimization
expert2:00remaining
Optimizing change stream pipeline for update operations
You want to watch only
update operations on the inventory collection where the quantity field was changed. Which pipeline is the most efficient to achieve this?Attempts:
2 left
💡 Hint
Combine conditions in a single $match stage for better performance.
✗ Incorrect
Using a single $match stage with both conditions reduces pipeline stages and improves efficiency.