Bird
Raised Fist0
MongoDBquery~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using change streams on a MongoDB collection?
easy
A. To create indexes on the collection
B. To backup the collection data automatically
C. To watch for real-time changes happening in the collection
D. To delete documents based on a condition

Solution

  1. Step 1: Understand what change streams do

    Change streams allow applications to listen for real-time changes like inserts, updates, or deletes in a collection.
  2. Step 2: Compare options with this purpose

    Only To watch for real-time changes happening in the collection describes watching for real-time changes, which matches the purpose of change streams.
  3. Final Answer:

    To watch for real-time changes happening in the collection -> Option C
  4. Quick Check:

    Change streams = watch real-time changes [OK]
Hint: Change streams = real-time watch on collection changes [OK]
Common Mistakes:
  • Thinking change streams backup data
  • Confusing change streams with indexing
  • Assuming change streams delete documents
2. Which of the following is the correct way to start a change stream on a collection named orders in MongoDB?
easy
A. db.orders.changeStream()
B. db.orders.watch()
C. db.orders.listen()
D. db.orders.observe()

Solution

  1. Step 1: Recall the MongoDB method for change streams

    The correct method to open a change stream on a collection is watch().
  2. Step 2: Match the method with the collection name

    Using db.orders.watch() correctly starts the change stream on the orders collection.
  3. Final Answer:

    db.orders.watch() -> Option B
  4. Quick Check:

    Change stream method = watch() [OK]
Hint: Use .watch() to start change streams on collections [OK]
Common Mistakes:
  • Using non-existent methods like changeStream()
  • Confusing with listen() or observe() which don't exist
  • Misspelling the method name
3. Given the following code snippet, what will be the output when a new document is inserted into the products collection?
const changeStream = db.products.watch();
changeStream.on('change', (change) => {
  console.log(change.operationType);
});
medium
A. 'insert'
B. 'update'
C. 'delete'
D. 'replace'

Solution

  1. Step 1: Understand the event listener on change stream

    The code listens for any change and logs the operationType of that change.
  2. Step 2: Identify the operation type for inserting a document

    When a new document is inserted, the operationType is insert.
  3. Final Answer:

    'insert' -> Option A
  4. Quick Check:

    Insert operation logs 'insert' [OK]
Hint: Insert triggers operationType 'insert' in change stream [OK]
Common Mistakes:
  • Confusing insert with update or delete
  • Expecting full document instead of operationType
  • Misreading event listener output
4. You wrote this code to watch changes on the users collection but it throws an error:
const changeStream = db.users.watch({ operationType: 'insert' });
What is the mistake?
medium
A. The operationType should be capitalized as 'Insert'
B. The collection name should be user not users
C. You must call start() after watch()
D. The watch method does not accept a filter object directly

Solution

  1. Step 1: Check the argument to watch()

    The watch() method expects an array pipeline for filtering, not a direct object.
  2. Step 2: Understand correct filtering syntax

    To filter by operationType, you must pass a pipeline like [{ $match: { operationType: 'insert' } }].
  3. Final Answer:

    The watch method does not accept a filter object directly -> Option D
  4. Quick Check:

    Filter must be pipeline array, not object [OK]
Hint: Use pipeline array for filters in watch() [OK]
Common Mistakes:
  • Passing filter as object instead of pipeline array
  • Wrong collection name assumption
  • Expecting a start() method
  • Capitalizing operationType values incorrectly
5. You want to build a live notification system that only alerts when a document in the tasks collection is updated and the status field changes to 'completed'. Which change stream pipeline correctly filters these changes?
hard
A. [{ $match: { operationType: 'update', 'updateDescription.updatedFields.status': 'completed' } }]
B. [{ $match: { operationType: 'insert', 'fullDocument.status': 'completed' } }]
C. [{ $match: { 'updateDescription.updatedFields.status': 'completed' } }]
D. [{ $match: { operationType: 'delete', 'updateDescription.updatedFields.status': 'completed' } }]

Solution

  1. Step 1: Identify the operation type needed

    We want to watch only updates, so operationType must be 'update'.
  2. Step 2: Filter for status field changed to 'completed'

    The updateDescription.updatedFields.status contains changed fields; we check if it equals 'completed'.
  3. Step 3: Combine filters in a pipeline

    The correct pipeline is [{ $match: { operationType: 'update', 'updateDescription.updatedFields.status': 'completed' } }].
  4. Final Answer:

    [{ $match: { operationType: 'update', 'updateDescription.updatedFields.status': 'completed' } }] -> Option A
  5. Quick Check:

    Filter updates with status='completed' = [{ $match: { operationType: 'update', 'updateDescription.updatedFields.status': 'completed' } }] [OK]
Hint: Filter update and updatedFields.status in $match pipeline [OK]
Common Mistakes:
  • Filtering inserts or deletes instead of updates
  • Checking fullDocument instead of updateDescription
  • Missing operationType filter
  • Using wrong field path in filter
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard