Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to write the aggregation results to a new collection using $out.
MongoDB
db.sales.aggregate([{ $group: { _id: "$item", total: { $sum: "$quantity" } } }, { $[1]: "totalSales" }]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $merge instead of $out when the goal is to replace the entire collection.
Using $project or $match which do not write results to collections.
✗ Incorrect
The $out stage writes the aggregation results to a specified collection, replacing it if it exists.
2fill in blank
mediumComplete the code to merge aggregation results into an existing collection without replacing it.
MongoDB
db.orders.aggregate([{ $group: { _id: "$customerId", totalSpent: { $sum: "$amount" } } }, { $[1]: { into: "customerTotals", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $out which replaces the entire collection instead of merging.
Using $lookup or $unwind which are unrelated to writing results.
✗ Incorrect
$merge allows merging aggregation results into an existing collection with control over matching and insertion behavior.
3fill in blank
hardFix the error in the aggregation pipeline to correctly write results using $merge with the right option key.
MongoDB
db.inventory.aggregate([{ $match: { status: "A" } }, { $merge: { [1]: "activeInventory" } }]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'to' or 'collection' which are not valid keys for $merge.
Using 'target' which is not recognized by MongoDB.
✗ Incorrect
The correct option key for specifying the target collection in $merge is 'into'.
4fill in blank
hardFill both blanks to write aggregation results to a collection named 'summary' and replace it if it exists.
MongoDB
db.transactions.aggregate([{ $group: { _id: "$type", count: { $sum: 1 } } }, { $[1]: "[2]" }]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $merge instead of $out when replacement is desired.
Using wrong collection names.
✗ Incorrect
$out writes results to the specified collection, replacing it. Here, 'summary' is the target collection.
5fill in blank
hardFill all three blanks to merge results into 'archive', matching on '_id', and updating existing documents.
MongoDB
db.logs.aggregate([{ $match: { level: "error" } }, { $[1]: { into: "[2]", on: "[3]", whenMatched: "merge", whenNotMatched: "insert" } }]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $out instead of $merge for merging.
Using wrong collection or field names.
✗ Incorrect
$merge stage merges documents into 'archive' collection matching on '_id' and merges existing documents.