📖 Scenario: You are managing a MongoDB database for a small online bookstore. You want to monitor changes to the orders collection, but only for orders where the status is "shipped". This helps you react quickly to shipped orders without processing all changes.
🎯 Goal: Build a MongoDB change stream pipeline that filters change events to only include those where the status field in the fullDocument is "shipped".
📋 What You'll Learn
Create a change stream pipeline array called pipeline with a match stage filtering fullDocument.status equal to "shipped".
Create a variable called changeStream that watches the orders collection using the pipeline.
Use the watch method with the pipeline to filter change events.
Add a final option to the watch method to include the full document in the change event.
💡 Why This Matters
🌍 Real World
Filtering change streams is useful in real-time applications like order tracking, notifications, and syncing data where only specific changes matter.
💼 Career
Understanding change stream pipelines is important for backend developers and database administrators working with MongoDB to build efficient, reactive systems.
Progress0 / 4 steps
1
Create the change stream pipeline array
Create a variable called pipeline and set it to an array containing one object. This object should be a $match stage that filters documents where fullDocument.status is exactly "shipped".
MongoDB
Hint
Use $match inside the array to filter change events by the status field inside fullDocument.
2
Create the change stream watcher
Create a variable called changeStream and assign it the result of calling db.orders.watch(pipeline).
MongoDB
Hint
Use the watch method on db.orders with the pipeline variable.
3
Add option to include full document in change events
Modify the changeStream assignment to call db.orders.watch(pipeline, { fullDocument: "updateLookup" }) so that the full updated document is included in change events.
MongoDB
Hint
Pass the option { fullDocument: "updateLookup" } as the second argument to watch.
4
Complete the change stream setup
Ensure the pipeline and changeStream variables are defined exactly as before, with the watch method using the pipeline and the option { fullDocument: "updateLookup" }.
MongoDB
Hint
Double-check that the pipeline and changeStream variables are exactly as required.
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
Step 1: Understand change streams
Change streams allow applications to listen to changes in MongoDB collections or databases.
Step 2: Role of $match in pipelines
The $match stage filters these change events so only specific changes are passed through.
Final Answer:
To filter change events and receive only relevant updates -> Option D
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
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.
Step 2: Match only insert events
The correct filter uses { $match: { operationType: 'insert' } } to watch only insert changes.
Final Answer:
{ $match: { operationType: 'insert' } } -> Option A
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?
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?