Bird
Raised Fist0
MongoDBquery~20 mins

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

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

Practice

(1/5)
1. What does the $push accumulator do in a MongoDB aggregation pipeline?
easy
A. It removes duplicate values from an array.
B. It adds values to an array for each group in the aggregation.
C. It sorts the documents in the aggregation result.
D. It counts the number of documents in each group.

Solution

  1. Step 1: Understand the purpose of $push

    The $push accumulator collects values into an array for each group during aggregation.
  2. Step 2: Compare with other accumulators

    Unlike $addToSet which removes duplicates, $push simply appends values, allowing duplicates.
  3. Final Answer:

    It adds values to an array for each group in the aggregation. -> Option B
  4. Quick Check:

    $push = add values to array [OK]
Hint: Remember: $push always appends values to arrays [OK]
Common Mistakes:
  • Confusing $push with $addToSet which removes duplicates
  • Thinking $push sorts the array automatically
  • Assuming $push counts documents instead of collecting values
2. Which of the following is the correct syntax to use $push in a MongoDB aggregation $group stage?
easy
A. { $group: { _id: "$category", items: { $push: { "$item" } } } }
B. { $group: { _id: "$category", items: { $push: ["$item"] } } }
C. { $group: { _id: "$category", items: { $push: "$item" } } }
D. { $group: { _id: "$category", items: { push: "$item" } } }

Solution

  1. Step 1: Review correct $push syntax

    The $push operator requires a field name with a value to push, like $push: "$item".
  2. Step 2: Identify syntax errors in options

    { $group: { _id: "$category", items: { $push: ["$item"] } } } incorrectly uses an array around "$item". { $group: { _id: "$category", items: { $push: { "$item" } } } } uses braces instead of quotes. { $group: { _id: "$category", items: { push: "$item" } } } misses the $ sign before push.
  3. Final Answer:

    { $group: { _id: "$category", items: { $push: "$item" } } } -> Option C
  4. Quick Check:

    Correct $push syntax = { $group: { _id: "$category", items: { $push: "$item" } } } [OK]
Hint: Use $push: "fieldName" inside $group stage [OK]
Common Mistakes:
  • Forgetting the $ before push
  • Using arrays or objects incorrectly inside $push
  • Misplacing quotes around field names
3. Given the collection orders with documents:
{ "customer": "Alice", "product": "Pen" }
{ "customer": "Alice", "product": "Notebook" }
{ "customer": "Bob", "product": "Pen" }

What will be the result of this aggregation?
{ $group: { _id: "$customer", products: { $push: "$product" } } }
medium
A. [{ "_id": "Alice", "products": ["Pen", "Notebook"] }, { "_id": "Bob", "products": ["Pen"] }]
B. [{ "_id": "Alice", "products": "Pen" }, { "_id": "Bob", "products": "Pen" }]
C. [{ "_id": "Alice", "products": ["Notebook"] }, { "_id": "Bob", "products": ["Pen"] }]
D. [{ "_id": "Alice", "products": [] }, { "_id": "Bob", "products": [] }]

Solution

  1. Step 1: Group documents by customer

    The aggregation groups documents by the "customer" field, so Alice and Bob form separate groups.
  2. Step 2: Use $push to collect products

    For each group, $push collects all "product" values into an array. Alice has "Pen" and "Notebook", Bob has "Pen".
  3. Final Answer:

    [{ "_id": "Alice", "products": ["Pen", "Notebook"] }, { "_id": "Bob", "products": ["Pen"] }] -> Option A
  4. Quick Check:

    Group by customer + $push products = [{ "_id": "Alice", "products": ["Pen", "Notebook"] }, { "_id": "Bob", "products": ["Pen"] }] [OK]
Hint: Group by field, $push collects all matching values in array [OK]
Common Mistakes:
  • Expecting $push to return single value instead of array
  • Confusing $push with $first or $last accumulator
  • Ignoring grouping effect on output structure
4. You wrote this aggregation but it throws an error:
{ $group: { _id: "$category", items: { $push: "$item" } } }

What is the likely cause of the error?
medium
A. The field names "category" or "item" do not exist in the documents.
B. You forgot to include the $match stage before $group.
C. The $push operator cannot be used inside $group.
D. The _id field cannot be a string.

Solution

  1. Step 1: Check field existence in documents

    If the fields "category" or "item" do not exist in the documents, $push will fail because it tries to access missing fields.
  2. Step 2: Validate other options

    $push is valid inside $group, $match is optional, and _id can be a string. So these are not causes of error.
  3. Final Answer:

    The field names "category" or "item" do not exist in the documents. -> Option A
  4. Quick Check:

    Missing fields cause $push error [OK]
Hint: Verify fields exist before using $push on them [OK]
Common Mistakes:
  • Assuming $push requires $match stage
  • Thinking _id cannot be string
  • Believing $push is invalid inside $group
5. You want to group sales by store and collect all product names sold, but only unique products without duplicates. Which aggregation pipeline correctly uses $push and another operator to achieve this?
hard
A. { $group: { _id: "$store", products: { $push: "$product" } } }
B. { $group: { _id: "$store", products: { $push: { $each: "$product" } } } }
C. { $group: { _id: "$store", products: { $push: { $addToSet: "$product" } } } }
D. { $group: { _id: "$store", products: { $addToSet: "$product" } } }

Solution

  1. Step 1: Understand $push vs $addToSet

    $push collects all values including duplicates. $addToSet collects unique values only.
  2. Step 2: Identify correct usage for unique array

    To get unique products per store, use $addToSet: "$product" inside $group.
  3. Step 3: Analyze other options

    { $group: { _id: "$store", products: { $push: "$product" } } } uses $push (duplicates allowed). { $group: { _id: "$store", products: { $push: { $addToSet: "$product" } } } } nests $addToSet inside $push which is invalid. { $group: { _id: "$store", products: { $push: { $each: "$product" } } } } misuses $each inside $push.
  4. Final Answer:

    { $group: { _id: "$store", products: { $addToSet: "$product" } } } -> Option D
  5. Quick Check:

    Unique array needs $addToSet, not $push [OK]
Hint: Use $addToSet for unique arrays, $push allows duplicates [OK]
Common Mistakes:
  • Using $push when unique values are needed
  • Nesting $addToSet inside $push incorrectly
  • Misusing $each inside $push accumulator
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard