Bird
Raised Fist0
MongoDBquery~20 mins

$out and $merge for writing results in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
MongoDB Write Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What does this $out stage do?
Consider this aggregation pipeline on a collection named sales:
[{ $match: { status: "completed" } }, { $out: "completed_sales" }]
What is the result after running this pipeline?
MongoDB
[{ $match: { status: "completed" } }, { $out: "completed_sales" }]
AIt creates or replaces the collection 'completed_sales' with documents where status is 'completed'.
BIt appends documents with status 'completed' to the existing 'completed_sales' collection.
CIt updates documents in 'completed_sales' to have status 'completed'.
DIt returns documents with status 'completed' without modifying any collection.
Attempts:
2 left
💡 Hint
Think about what $out does to the target collection.
❓ query_result
intermediate
2:00remaining
What happens when using $merge with 'whenMatched: replace'?
Given this pipeline:
[{ $match: { category: "books" } }, { $merge: { into: "inventory", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }]

What is the effect on the 'inventory' collection?
MongoDB
[{ $match: { category: "books" } }, { $merge: { into: "inventory", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }]
AAll documents are appended to 'inventory' without checking _id.
BDocuments with matching _id are merged by adding fields; new documents are ignored.
CDocuments with matching _id are replaced; new documents are inserted into 'inventory'.
DDocuments with matching _id are updated by incrementing fields; new documents are rejected.
Attempts:
2 left
💡 Hint
Check the meaning of 'whenMatched: replace' and 'whenNotMatched: insert'.
📝 Syntax
advanced
2:00remaining
Which $merge option syntax is invalid?
Identify the option that will cause a syntax error when used in a $merge stage:
MongoDB
{ $merge: { into: "users", on: "userId", whenMatched: "merge", whenNotMatched: "insert" } }
A{ $merge: { into: "users", on: ["userId", "email"], whenMatched: "keepExisting", whenNotMatched: "insert" } }
B{ $merge: { into: "users", on: "userId", whenMatched: "update", whenNotMatched: "insert" } }
C{ $merge: { into: "users", on: "userId", whenMatched: "fail", whenNotMatched: "discard" } }
D{ $merge: { into: "users", on: "userId", whenMatched: "merge", whenNotMatched: "insert" } }
Attempts:
2 left
💡 Hint
Check the valid values for 'whenMatched'.
❓ optimization
advanced
2:00remaining
Optimizing $merge for large datasets
You want to merge a large aggregation result into a collection without replacing entire documents, only updating specific fields. Which $merge option is best for this?
AUse 'whenMatched: "merge"' to update only specified fields and 'whenNotMatched: "insert"'.
BUse 'whenMatched: "replace"' to replace entire documents for consistency.
CUse 'whenMatched: "fail"' to avoid any updates and only insert new documents.
DUse 'whenMatched: "keepExisting"' to keep old documents and ignore new data.
Attempts:
2 left
💡 Hint
Think about updating only parts of documents efficiently.
🧠 Conceptual
expert
3:00remaining
Difference between $out and $merge in concurrency
Which statement best describes the difference between $out and $merge regarding concurrent writes and atomicity?
ABoth <code>$out</code> and <code>$merge</code> block all concurrent writes until operation completes.
B<code>$merge</code> atomically replaces the entire target collection; <code>$out</code> merges documents individually allowing concurrent writes.
CNeither <code>$out</code> nor <code>$merge</code> guarantee atomicity or affect concurrent writes.
D<code>$out</code> atomically replaces the entire target collection, blocking concurrent writes; <code>$merge</code> allows concurrent writes by merging documents individually.
Attempts:
2 left
💡 Hint
Consider how each stage writes results to the target collection.

Practice

(1/5)
1. What is the main difference between the $out and $merge stages in MongoDB aggregation?
easy
A. $out updates documents, and $merge deletes the target collection.
B. $out only works with sharded clusters, $merge does not.
C. $out replaces the entire target collection, while $merge updates or inserts documents based on matching keys.
D. $out appends documents without replacing, $merge replaces the collection.

Solution

  1. Step 1: Understand $out behavior

    $out writes aggregation results by replacing the entire target collection with new data.
  2. Step 2: Understand $merge behavior

    $merge writes results by updating existing documents or inserting new ones based on matching keys, preserving unmatched documents.
  3. Final Answer:

    $out replaces collection; $merge updates/inserts by keys -> Option C
  4. Quick Check:

    $out replaces, $merge merges [OK]
Hint: Remember: $out replaces, $merge merges by keys [OK]
Common Mistakes:
  • Thinking $out appends data instead of replacing
  • Confusing $merge with deleting collections
  • Assuming $merge always replaces entire collection
2. Which of the following is the correct syntax to use $out in a MongoDB aggregation pipeline?
easy
A. { $out: "targetCollection" }
B. { $out: { collection: "targetCollection" } }
C. { $out: ["targetCollection"] }
D. { $out: { db: "targetDB" } }

Solution

  1. Step 1: Recall $out syntax

    The $out stage requires the target collection name as a string directly, like { $out: "collectionName" }.
  2. Step 2: Check options

    { $out: "targetCollection" } matches the correct syntax. The other options use incorrect formats such as extra objects or arrays.
  3. Final Answer:

    { $out: "targetCollection" } -> Option A
  4. Quick Check:

    $out needs collection name string [OK]
Hint: Use simple string for collection name in $out [OK]
Common Mistakes:
  • Using object or array instead of string for collection name
  • Trying to specify database inside $out stage
  • Adding extra braces or brackets around collection name
3. Given the aggregation pipeline below, what will be the result in the summary collection after running it?
db.sales.aggregate([
  { $group: { _id: "$category", total: { $sum: "$amount" } } },
  { $merge: { into: "summary", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }
])
medium
A. The summary collection will remain unchanged.
B. The summary collection will be completely replaced with the aggregation results.
C. The aggregation will fail due to missing $out stage.
D. The summary collection will contain documents with total sales per category, replacing existing matching documents.

Solution

  1. Step 1: Understand the $group stage

    This groups sales by category and sums amounts, producing documents like { _id: category, total: sum }.
  2. Step 2: Understand the $merge stage

    $merge writes results into summary collection, replacing documents with matching _id or inserting new ones.
  3. Final Answer:

    summary collection updated with total sales per category, replacing matches -> Option D
  4. Quick Check:

    $merge updates or inserts by key [OK]
Hint: Remember $merge updates matching keys, inserts new [OK]
Common Mistakes:
  • Thinking $merge replaces entire collection like $out
  • Assuming aggregation fails without $out
  • Believing collection stays unchanged after $merge
4. You run this aggregation pipeline but get an error:
db.orders.aggregate([
  { $match: { status: "shipped" } },
  { $out: { collection: "shippedOrders" } }
])

What is the likely cause of the error?
medium
A. You cannot use $out after $match stage.
B. The $out stage syntax is incorrect; it should be a string, not an object.
C. The $match stage is invalid and causes the error.
D. The target collection shippedOrders does not exist.

Solution

  1. Step 1: Check $out syntax

    $out requires the target collection name as a string, not an object with a collection field.
  2. Step 2: Analyze error cause

    Using { collection: "shippedOrders" } causes syntax error; correct usage is { $out: "shippedOrders" }.
  3. Final Answer:

    Incorrect $out syntax using object instead of string -> Option B
  4. Quick Check:

    $out needs string collection name [OK]
Hint: Use string for $out target, not object [OK]
Common Mistakes:
  • Assuming $match causes error
  • Thinking $out requires existing collection
  • Believing $out cannot follow $match
5. You want to aggregate user activity data and save results into a collection named userStats. You want to update existing documents by userId and insert new ones if missing, but keep other fields unchanged when matched. Which $merge option should you use?
hard
A. { into: "userStats", on: "userId", whenMatched: "merge", whenNotMatched: "insert" }
B. { into: "userStats", on: "userId", whenMatched: "replace", whenNotMatched: "insert" }
C. { into: "userStats", on: "userId", whenMatched: "fail", whenNotMatched: "insert" }
D. { into: "userStats", on: "userId", whenMatched: "keepExisting", whenNotMatched: "discard" }

Solution

  1. Step 1: Understand whenMatched options

    whenMatched: "merge" updates only specified fields, keeping others unchanged; replace overwrites entire document.
  2. Step 2: Choose correct $merge options

    To update existing documents by userId and insert new ones, use whenMatched: "merge" and whenNotMatched: "insert".
  3. Final Answer:

    { into: "userStats", on: "userId", whenMatched: "merge", whenNotMatched: "insert" } -> Option A
  4. Quick Check:

    whenMatched: "merge" updates fields, inserts new [OK]
Hint: Use whenMatched: "merge" to update fields, not replace [OK]
Common Mistakes:
  • Using replace which overwrites entire documents
  • Choosing fail which errors on matches
  • Using invalid whenNotMatched options
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