0
0
MongoDBquery~20 mins

Change streams on collections 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 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' }
AA change event document with operationType 'delete' and documentKey { _id: 1 }
BA change event document with operationType 'insert' and fullDocument containing { _id: 1, name: 'Alice' }
CNo change event will be emitted because inserts are not captured
DA change event document with operationType 'update' and fullDocument containing { _id: 1, name: 'Alice' }
Attempts:
2 left
💡 Hint
Change streams capture insert operations and include the full inserted document.
query_result
intermediate
2: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);
APipeline with { $match: { 'fullDocument.status': 'shipped' } }
BPipeline with { $match: { operationType: 'update', 'updateDescription.updatedFields.status': 'shipped' } }
CPipeline with { $match: { 'documentKey.status': 'shipped' } }
DPipeline with { $match: { status: 'shipped' } }
Attempts:
2 left
💡 Hint
The fullDocument field contains the entire changed document after the operation.
schema
advanced
2:00remaining
Required collection configuration for change streams
Which of the following is required for a MongoDB collection to support change streams?
AThe collection must be a capped collection
BThe collection must have a unique index on the _id field
CThe collection must have a TTL index
DThe collection must be part of a replica set or a sharded cluster
Attempts:
2 left
💡 Hint
Change streams rely on the oplog which exists only in replica sets or sharded clusters.
🔧 Debug
advanced
2: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?
AThe MongoDB deployment is a standalone server, not a replica set or sharded cluster
BThe pipeline is missing a $project stage
CThe change stream cursor was not iterated with next() or on('change')
DThe insert operations are not supported by change streams
Attempts:
2 left
💡 Hint
Change streams require a replica set or sharded cluster to work.
optimization
expert
2: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?
A[{ $match: { 'updateDescription.updatedFields.quantity': { $exists: true } } }]
B[{ $match: { operationType: 'update' } }, { $match: { 'updateDescription.updatedFields.quantity': { $exists: true } } }]
C[{ $match: { operationType: 'update', 'updateDescription.updatedFields.quantity': { $exists: true } } }]
D[{ $match: { operationType: 'update', 'fullDocument.quantity': { $exists: true } } }]
Attempts:
2 left
💡 Hint
Combine conditions in a single $match stage for better performance.