$push accumulator for building arrays in MongoDB
📖 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