0
0
MongoDBquery~10 mins

$out and $merge for writing results in MongoDB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Amerge
Bmatch
Cproject
Dout
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.
2fill in blank
medium

Complete 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'
Aout
Blookup
Cmerge
Dunwind
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.
3fill in blank
hard

Fix 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'
Ato
Binto
Ccollection
Dtarget
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.
4fill in blank
hard

Fill 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'
Aout
Bmerge
Csummary
Dtotals
Attempts:
3 left
💡 Hint
Common Mistakes
Using $merge instead of $out when replacement is desired.
Using wrong collection names.
5fill in blank
hard

Fill 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'
Amerge
Barchive
C_id
Dout
Attempts:
3 left
💡 Hint
Common Mistakes
Using $out instead of $merge for merging.
Using wrong collection or field names.