Change stream pipelines for filtering in MongoDB - Time & Space Complexity
When using change stream pipelines in MongoDB, it's important to understand how filtering affects performance.
We want to know how the time to process changes grows as more data flows through the pipeline.
Analyze the time complexity of this change stream pipeline with filtering.
const pipeline = [
{ $match: { 'fullDocument.status': 'active' } },
{ $project: { _id: 0, name: 1, status: 1 } }
];
const changeStream = collection.watch(pipeline);
changeStream.on('change', (change) => {
console.log(change);
});
This code listens for changes where the document's status is 'active' and only returns selected fields.
Look at what repeats as changes happen:
- Primary operation: Filtering each change event with the $match stage.
- How many times: Once per change event received from the database.
As more changes occur, the pipeline filters each one individually.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 filter checks |
| 100 | 100 filter checks |
| 1000 | 1000 filter checks |
Pattern observation: The number of filter operations grows directly with the number of changes.
Time Complexity: O(n)
This means the time to process changes grows linearly with the number of change events.
[X] Wrong: "Filtering in the pipeline makes processing time constant regardless of changes."
[OK] Correct: Each change event still needs to be checked against the filter, so time grows with the number of events.
Understanding how filtering affects change streams helps you explain real-time data handling and efficiency in applications.
What if we added multiple $match stages in the pipeline? How would the time complexity change?