0
0
MongoDBquery~5 mins

$out and $merge for writing results in MongoDB - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: $out and $merge for writing results
O(n)
Understanding Time 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?

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

As the number of orders grows, the work to scan and group grows too.

Input Size (n)Approx. Operations
10About 10 reads and writes for groups
100About 100 reads and writes for groups
1000About 1000 reads and writes for groups

Pattern observation: The operations grow roughly in direct proportion to the number of input documents.

Final Time Complexity

Time Complexity: O(n)

This means the time to process and write results grows linearly with the number of input documents.

Common Mistake

[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.

Interview Connect

Understanding how writing results scales helps you explain performance in real projects and shows you know how MongoDB handles big data.

Self-Check

What if we changed $merge to update only existing documents without inserting new ones? How would the time complexity change?