Bird
Raised Fist0
MongoDBquery~20 mins

Change stream pipelines for filtering in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Change Stream Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Filter change stream to only capture insert operations
Given a MongoDB change stream pipeline, which option correctly filters to capture only insert operations?
MongoDB
const pipeline = [
  { $match: { 'operationType': 'insert' } }
];

const changeStream = collection.watch(pipeline);
A[{ $match: { 'fullDocument': { $exists: false } } }]
B[{ $match: { 'operationType': { $ne: 'insert' } } }]
C[{ $match: { 'operationType': 'insert' } }]
D[{ $match: { 'operationType': 'update' } }]
Attempts:
2 left
💡 Hint
Look for the operationType field and match it exactly to 'insert'.
❓ query_result
intermediate
2:00remaining
Filter change stream for updates on a specific field
Which pipeline filters a MongoDB change stream to capture only update operations where the field status was changed?
MongoDB
const pipeline = [
  { $match: { 'operationType': 'update', 'updateDescription.updatedFields.status': { $exists: true } } }
];
A[{ $match: { 'operationType': 'update', 'fullDocument.status': { $exists: true } } }]
B[{ $match: { 'operationType': 'update', 'updateDescription.removedFields': 'status' } }]
C[{ $match: { 'operationType': 'insert', 'updateDescription.updatedFields.status': { $exists: true } } }]
D[{ $match: { 'operationType': 'update', 'updateDescription.updatedFields.status': { $exists: true } } }]
Attempts:
2 left
💡 Hint
Check the updateDescription.updatedFields object for the changed fields.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this change stream pipeline
Which option contains a syntax error in the MongoDB change stream pipeline?
MongoDB
const pipeline = [
  { $match: { operationType: 'delete' } },
  { $project: { documentKey: 1, _id: 0 } }
];
A[{ $match: { operationType: 'delete' } }, { $project: { documentKey: 1 _id: 0 } }]
B[{ $match: { 'operationType': 'delete' } }, { $project: { documentKey: 1, _id: 0 } }]
C[{ $match: { operationType: 'delete' } }, { $project: { documentKey: 1, _id: 0 } }]
D]} } 0 :di_ ,1 :yeKtnemucod { :tcejorp$ { ,} } 'eteled' :epyTnoitarepo { :hctam$ {[
Attempts:
2 left
💡 Hint
Look carefully at the commas separating fields inside $project.
❓ optimization
advanced
2:00remaining
Optimize change stream pipeline to reduce unnecessary data
Which pipeline best optimizes a change stream to only return the _id and changed fields for update operations?
A[{ $match: { operationType: 'update' } }, { $project: { _id: 0 } }]
B[{ $match: { operationType: 'update' } }, { $project: { 'updateDescription.updatedFields': 1, 'documentKey._id': 1, _id: 0 } }]
C[{ $match: { operationType: 'update' } }, { $project: { updateDescription: 1, _id: 0 } }]
D[{ $match: { operationType: 'update' } }, { $project: { fullDocument: 1, _id: 0 } }]
Attempts:
2 left
💡 Hint
Focus on projecting only the changed fields and the document ID.
🧠 Conceptual
expert
3:00remaining
Understanding change stream pipeline behavior with multiple $match stages
Consider this change stream pipeline with two $match stages:
[{ $match: { operationType: 'insert' } }, { $match: { 'fullDocument.status': 'active' } }]

What is the effect of using two separate $match stages instead of combining them into one?
AThe pipeline filters first for inserts, then filters those inserts for documents where status is 'active', effectively the same as a combined $match.
BThe pipeline will fail because multiple $match stages are not allowed in change stream pipelines.
CThe pipeline will return all inserts regardless of status because only the first $match is applied.
DThe pipeline will only filter by the last $match stage, ignoring the first.
Attempts:
2 left
💡 Hint
Think about how MongoDB pipelines process stages sequentially.

Practice

(1/5)
1. What is the main purpose of using a $match stage in a MongoDB change stream pipeline?
easy
A. To insert new documents into the collection
B. To create indexes on the collection
C. To delete documents from the collection
D. To filter change events and receive only relevant updates

Solution

  1. Step 1: Understand change streams

    Change streams allow applications to listen to changes in MongoDB collections or databases.
  2. Step 2: Role of $match in pipelines

    The $match stage filters these change events so only specific changes are passed through.
  3. Final Answer:

    To filter change events and receive only relevant updates -> Option D
  4. Quick Check:

    $match filters events [OK]
Hint: Remember: $match filters events to reduce noise [OK]
Common Mistakes:
  • Thinking $match modifies data instead of filtering
  • Confusing $match with data insertion or deletion
  • Assuming $match creates indexes
2. Which of the following is the correct syntax to filter change stream events for only insert operations using $match?
easy
A. { $match: { operationType: 'insert' } }
B. { $match: { operationType: 'replace' } }
C. { $match: { operationType: 'delete' } }
D. { $match: { operationType: 'update' } }

Solution

  1. Step 1: Identify operationType for inserts

    MongoDB change streams use operationType to specify the type of change, and 'insert' is the value for insert operations.
  2. Step 2: Match only insert events

    The correct filter uses { $match: { operationType: 'insert' } } to watch only insert changes.
  3. Final Answer:

    { $match: { operationType: 'insert' } } -> Option A
  4. Quick Check:

    Filter inserts with operationType 'insert' [OK]
Hint: Match operationType: 'insert' to filter inserts [OK]
Common Mistakes:
  • Using wrong operationType values like 'update' or 'delete'
  • Missing quotes around 'insert'
  • Using $match incorrectly outside pipeline
3. Given the following change stream pipeline, what events will be received?
[ { $match: { 'fullDocument.status': 'active' } } ]
medium
A. All change events regardless of document status
B. Only delete events
C. Only change events where the updated document has status 'active'
D. Only insert events with status 'inactive'

Solution

  1. Step 1: Understand the filter condition

    The pipeline filters events where the fullDocument.status field equals 'active'.
  2. Step 2: Effect on change events

    This means only events where the changed document's status is 'active' will pass through.
  3. Final Answer:

    Only change events where the updated document has status 'active' -> Option C
  4. Quick Check:

    Filter by fullDocument.status = 'active' [OK]
Hint: Filter on fullDocument fields to get specific document changes [OK]
Common Mistakes:
  • Assuming all events pass through without filtering
  • Confusing delete events which don't have fullDocument
  • Thinking it filters by operationType
4. You wrote this change stream pipeline to watch only delete events:
[ { $match: { operationType: 'delete' } } ]

But you still receive update events. What is the likely mistake?
medium
A. The field name should be operation_type instead of operationType
B. The pipeline is not applied correctly or the driver ignores the filter
C. The $match stage is missing in the pipeline
D. Delete events do not have an operationType field

Solution

  1. Step 1: Verify field name and filter correctness

    The field operationType is correct and the filter matches delete events properly.
  2. Step 2: Consider pipeline application

    If update events still appear, the pipeline might not be applied correctly or the driver ignores the filter.
  3. Final Answer:

    The pipeline is not applied correctly or the driver ignores the filter -> Option B
  4. Quick Check:

    Incorrect pipeline application causes wrong events [OK]
Hint: Check if pipeline is properly passed to watch() method [OK]
Common Mistakes:
  • Using wrong field name like operation_type
  • Assuming delete events lack operationType
  • Not passing pipeline to watch() function
5. You want to watch changes only for documents where category is 'books' and the operation is either 'insert' or 'update'. Which pipeline correctly filters these events?
hard
A. [ { $match: { $and: [ { 'fullDocument.category': 'books' }, { operationType: { $in: ['insert', 'update'] } } ] } } ]
B. [ { $match: { 'fullDocument.category': 'books', operationType: 'insert' } }, { $match: { operationType: 'update' } } ]
C. [ { $match: { operationType: 'insert' } }, { $match: { 'fullDocument.category': 'books' } } ]
D. [ { $match: { $or: [ { 'fullDocument.category': 'books' }, { operationType: 'insert' }, { operationType: 'update' } ] } } ]

Solution

  1. Step 1: Combine conditions with $and

    We want documents where category is 'books' AND operationType is 'insert' or 'update'.
  2. Step 2: Use $in for multiple operationTypes

    The correct way is to use $in inside $and to filter operationType for both 'insert' and 'update'.
  3. Final Answer:

    [ { $match: { $and: [ { 'fullDocument.category': 'books' }, { operationType: { $in: ['insert', 'update'] } } ] } } ] -> Option A
  4. Quick Check:

    Use $and with $in to combine filters [OK]
Hint: Use $and with $in to filter multiple conditions together [OK]
Common Mistakes:
  • Using multiple $match stages that override each other
  • Using $or instead of $and for combined conditions
  • Not using $in for multiple operationTypes
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