$out and $merge for writing results in MongoDB - Time & Space Complexity
When using $out and $merge in MongoDB, we want to know how the time to write results changes as the data grows.
We ask: How does the cost of saving results scale with more data?
Analyze the time complexity of the following MongoDB aggregation using $merge.
db.orders.aggregate([
{ $match: { status: "completed" } },
{ $group: { _id: "$customerId", total: { $sum: "$amount" } } },
{ $merge: { into: "customerTotals", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }
])
This groups orders by customer and writes the totals into another collection using $merge.
Look for repeated work inside the aggregation.
- Primary operation: Scanning each order document once to group by customer.
- How many times: Once per document in the input collection.
- Write operation: Writing grouped results to the target collection, once per group.
As the number of orders grows, the work to scan and group grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 reads and writes for groups |
| 100 | About 100 reads and writes for groups |
| 1000 | About 1000 reads and writes for groups |
Pattern observation: The operations grow roughly in direct proportion to the number of input documents.
Time Complexity: O(n)
This means the time to process and write results grows linearly with the number of input documents.
[X] Wrong: "Using $merge or $out is instant and does not depend on data size."
[OK] Correct: Writing results requires touching each group or document, so more data means more work and time.
Understanding how writing results scales helps you explain performance in real projects and shows you know how MongoDB handles big data.
What if we changed $merge to update only existing documents without inserting new ones? How would the time complexity change?