📖 Scenario: You are managing a small online bookstore database. Each book document stores information about the book and a list of reviews from customers. You want to group all reviews by book title to see all feedback in one place.
🎯 Goal: Build an aggregation query using the $push accumulator to collect all reviews for each book title into an array.
📋 What You'll Learn
Create a collection named books with documents containing title and review fields.
Define a variable groupStage for the aggregation $group stage.
Use the $push accumulator inside groupStage to collect reviews into an array for each book title.
Write the complete aggregation pipeline using db.books.aggregate() with the groupStage.
💡 Why This Matters
🌍 Real World
Collecting and grouping user reviews or comments by product or article is common in many applications like e-commerce or blogs.
💼 Career
Understanding aggregation with $push is essential for data analysis and reporting tasks in MongoDB database roles.
Progress0 / 4 steps
1
Create the books collection with sample documents
Create a variable called books and assign it an array with these exact documents: { title: "Book A", review: "Great read" }, { title: "Book B", review: "Informative" }, { title: "Book A", review: "Loved it" }
MongoDB
Hint
Use an array of objects with exact title and review fields as shown.
2
Define the groupStage for aggregation
Create a variable called groupStage and assign it an object with a $group key. Group by _id: "$title" and prepare to use $push to collect reviews.
MongoDB
Hint
Use $group with _id set to "$title" and reviews using $push on "$review".
3
Write the aggregation pipeline using groupStage
Create a variable called pipeline and assign it an array containing the groupStage object.
MongoDB
Hint
Put the groupStage object inside an array assigned to pipeline.
4
Complete the aggregation query using db.books.aggregate()
Write a line of code that calls db.books.aggregate(pipeline) and assign the result to a variable called result.
MongoDB
Hint
Call db.books.aggregate(pipeline) and assign it to result.
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
Step 1: Understand the purpose of $push
The $push accumulator collects values into an array for each group during aggregation.
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
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.
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.
Final Answer:
The field names "category" or "item" do not exist in the documents. -> Option A
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?