0
0
MongoDBquery~5 mins

$group stage for aggregation in MongoDB - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the $group stage in MongoDB aggregation?
The $group stage groups documents by a specified key and allows you to perform calculations like sums, averages, or counts on each group.
Click to reveal answer
beginner
In the $group stage, what does the _id field represent?
The _id field defines the key to group documents by. Each unique value of _id creates a separate group.
Click to reveal answer
beginner
How do you count the number of documents in each group using $group?
Use { $sum: 1 } inside the $group stage to add 1 for each document, effectively counting them.
Click to reveal answer
intermediate
What aggregation operators can be used inside $group to calculate totals or averages?
You can use $sum to add values, $avg to calculate averages, $max for maximum, and $min for minimum values.
Click to reveal answer
intermediate
Can $group stage change the shape of documents?
Yes, $group outputs one document per group with fields you define, so the shape can be very different from the original documents.
Click to reveal answer
What does the _id field in $group specify?
AThe name of the collection
BThe output document's unique identifier
CThe sorting order
DThe field to group documents by
Which operator counts documents in each group?
A$sum: 1
B$avg
C$max
D$min
Which operator calculates the average value in $group?
A$count
B$sum
C$avg
D$group
Can $group stage output documents with different fields than the input?
AYes, it creates new documents per group
BNo, it keeps the same fields
COnly if you use <code>$project</code>
DOnly if you use <code>$match</code>
Which of these is NOT a valid accumulator operator in $group?
A$sum
B$concat
C$max
D$avg
Explain how the $group stage works in MongoDB aggregation.
Think about how you would group items in real life, like grouping fruits by type and counting them.
You got /3 concepts.
    Describe how to count the number of documents in each group using $group.
    Counting is like adding one for each item in the group.
    You got /2 concepts.