0
0
MongoDBquery~20 mins

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

Choose your learning style9 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.