group is one of the stage of aggregation.
group operator in aggregation is used to group similar documents and can perform operation on each document separately . These operations are represented by accumulators. The result of each accumulator is stored in document field.
Criteria to group similar documents is specified in the _id field. We will see in the below examples how _id field is used to group the documents.
group stage limits the number of documents in pipeline. Hence, it returns as many documents as number of groups.
Syntax: $group: {
_id: <expression>
field1: {<accumulator1>, <expression1> },
field2: {<accumulator2>, <expression2> },
.
fieldN: {<accumulator3>, <expressionN> }
}
}
Let us see one example to understand how group stage works.
Consider below books db
{
“books”: [
{
_id: 1,
name: “math_book_1”,
subject: “Math”,
condition: “good”,
alloted: false
},
{
_id: 2,
name: “math_book_2”,
subject: “Math”,
condition: “good”,
alloted: true
},
{
_id: 3,
name: “science_book_2”,
subject: “Science”,
condition: “good”,
alloted: false
}
]
}
Suppose we want to count the number of books of each subject using group, then it will be likedb.books.aggregate([
{
$group: {
_id: "$subject",
count: { $sum: 1 }
}
}
]
)
Output:[
{
"_id": "Science",
"count": 1
},
{
"_id": "Math",
"count": 2
}
]
Since we grouped based on the subject, so _id in the output will be subject name and count is number of books of that particular subject.
Here $sum is the accumulator used.