Challenge - 5 Problems
MongoDB $push Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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" }
}}
])Attempts:
2 left
💡 Hint
The $push accumulator collects values from each grouped document into an array in the order they appear.
✗ Incorrect
The $group stage groups documents by the 'item' field. For each group, $push collects the 'qty' values into an array preserving order. For 'apple', qty values 5 and 15 are pushed in order. For 'banana', only qty 10 exists.
🧠 Conceptual
intermediate1:00remaining
What does the $push accumulator do in MongoDB aggregation?
Choose the best description of the $push accumulator in MongoDB aggregation framework.
Attempts:
2 left
💡 Hint
Think about how $push collects values during grouping.
✗ Incorrect
$push collects values from each document in a group into an array, preserving the order they appear in the input documents.
📝 Syntax
advanced2: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?Attempts:
2 left
💡 Hint
Remember $push collects values as they are; $unwind is a separate stage.
✗ Incorrect
Option A correctly groups by author and pushes the entire tags array from each document into an array of arrays. Options A, C, and D misuse $unwind or $addToSet inside $group stage syntax.
🔧 Debug
advanced2:00remaining
Why does this aggregation pipeline fail?
Given this pipeline:
The query returns an error. What is the cause?
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?
Attempts:
2 left
💡 Hint
Check how fields are referenced in $project stage.
✗ Incorrect
In $project, to rename _id to store, you must use store: "$_id". This syntax is correct, so the error is likely elsewhere. But here, the error is a common confusion: MongoDB expects field names without quotes in $project. Using quotes around '$_id' causes an error.
❓ optimization
expert3: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?Attempts:
2 left
💡 Hint
Think about how to flatten arrays inside aggregation pipeline.
✗ Incorrect
Using $push on an array field creates an array of arrays. To flatten, you can use $reduce with $concatArrays in a $project stage after grouping. $addToSet does not flatten arrays automatically. $unwind after grouping is inefficient and changes document count.