0
0
MongoDBquery~20 mins

$push accumulator for building arrays in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB $push Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this aggregation using $push?
Consider a collection orders with documents:
{"_id": 1, "item": "apple", "qty": 5}
{"_id": 2, "item": "banana", "qty": 10}
{"_id": 3, "item": "apple", "qty": 15}
What will be the result of this aggregation?
db.orders.aggregate([
{ $group: {
_id: "$item",
quantities: { $push: "$qty" }
}}
])
MongoDB
db.orders.aggregate([
  { $group: {
      _id: "$item",
      quantities: { $push: "$qty" }
  }}
])
A[ { _id: "apple", quantities: [5, 15] }, { _id: "banana", quantities: [10] } ]
B[ { _id: "apple", quantities: [5] }, { _id: "banana", quantities: [10, 15] } ]
C[ { _id: "apple", quantities: 20 }, { _id: "banana", quantities: 10 } ]
D[ { _id: "apple", quantities: [15, 5] }, { _id: "banana", quantities: [10] } ]
Attempts:
2 left
💡 Hint
The $push accumulator collects values from each grouped document into an array in the order they appear.
🧠 Conceptual
intermediate
1:00remaining
What does the $push accumulator do in MongoDB aggregation?
Choose the best description of the $push accumulator in MongoDB aggregation framework.
AIt returns the first value of a field in each group.
BIt removes duplicate values from an array.
CIt sums numeric values for each group.
DIt adds values to an array for each group, preserving the order of documents.
Attempts:
2 left
💡 Hint
Think about how $push collects values during grouping.
📝 Syntax
advanced
2:00remaining
Which aggregation pipeline stage correctly uses $push to collect tags?
Given a collection posts with documents containing a tags array, which pipeline stage correctly groups by author and pushes all tags into one array?
A{ $group: { _id: "$author", allTags: { $push: "$tags" } } }
B{ $group: { _id: "$author", allTags: { $push: "$tags" }, $unwind: "$tags" } }
C{ $group: { _id: "$author", allTags: { $push: { $unwind: "$tags" } } } }
D{ $group: { _id: "$author", allTags: { $push: "$tags" }, $addToSet: "$tags" } }
Attempts:
2 left
💡 Hint
Remember $push collects values as they are; $unwind is a separate stage.
🔧 Debug
advanced
2:00remaining
Why does this aggregation pipeline fail?
Given this pipeline:
db.sales.aggregate([
{ $group: {
_id: "$store",
items: { $push: "$item" },
total: { $sum: "$amount" }
} },
{ $project: { _id: 0, store: "$_id", items: 1, total: 1 } }
])

The query returns an error. What is the cause?
AThe $project stage tries to rename '_id' to 'store' but must use $set instead.
BThe $project stage uses an invalid field name 'store' referencing '$_id' incorrectly.
CThe $group stage uses $push and $sum together which is not allowed.
DThe $group stage is missing a closing brace.
Attempts:
2 left
💡 Hint
Check how fields are referenced in $project stage.
optimization
expert
3:00remaining
How to optimize $push usage to avoid nested arrays when pushing array fields?
You have documents with a field tags which is an array. You want to group by category and collect all tags into a single flat array (not array of arrays). Which approach is best?
AUse $push: "$tags" in $group and accept nested arrays; flatten in application code.
BUse $addToSet: "$tags" in $group to avoid duplicates and flatten arrays automatically.
CUse $push: "$tags" in $group, then use $reduce or $concatArrays in a $project stage to flatten.
DUse $push: "$tags" in $group, then $unwind the resulting array after grouping.
Attempts:
2 left
💡 Hint
Think about how to flatten arrays inside aggregation pipeline.