Skip to content

MongoDB $group (aggregation) Usage with Examples AQSA Spark By {Examples}

  • by

The $group stage in MongoDB is used for grouping documents based on a specified key(s) in aggregation. The $group stage takes one or more fields as input and returns a new set of documents that have been grouped based on those fields. In this article, we will discuss the $group stage using different examples with the following collection.

#Create Collection
db.student.insertMany([
{
_id: 1,
name: “Jimmy”,
age: 24,
batch: 2022,
email: “jimmy@gmail.com”,
marks: 75,
date: ISODate(“2022-01-01T00:00:00Z”)

},
{
_id: 2,
name: “Elena”,
age: 20,
batch: 2021,
email: “elena@gmail.com”,
marks: 80,
date: ISODate(“2021-02-02T00:00:00Z”)
},
{
_id: 3,
name: “Caroline”,
age: 25,
batch: 2020,
email: “caroline@gmail.com”,
marks: 95,
date: ISODate(“2020-06-01T00:00:00Z”)
},
{
_id: 4,
name: “Elan”,
age: 23,
batch: 2023,
email: “elan@gmail.com”,
marks: 60,
date: ISODate(“2023-01-23T00:00:00Z”)
}
]
)
)

1. Syntax of $group stage in MongoDB

The syntax of the $group stage in MongoDB is provided below.

#Syntax of $group
{
$group:
{
_id: group_key
field: { accumulator_operator : expression },….
}
}
)

Here, group_key is the field we want to group by, the accumulator_operator is one of the accumulator operators listed below, and expression is the expression to apply the accumulator operator.

2. Group by a field and get the number of documents by using $group stage in MongoDB

Firstly, we have the group accumulator operator $count. The $count operator is used to generate the number the documents in a group. Here, we have the aggregation where the $count accumulator operator recorded the number of documents in each group and the $group operator grouped the documents by the name field. Since we are passing an empty object {} as an argument to $count, it will count the number of documents in each group.

#Usage of $count operator
db.student.aggregate( [
{
$group: {
_id: “$name”,
countNumberOfDocuments: {
$count: {}
}
}
}
] )
)

The following output group is the field below and count the documents for each specific group field.

3. Group by a field and add a set of unique value

Sometimes, we may need to group the fields and adds unique values to an array in a group. The $addToSet accumulator operator is used to insert the unique set of values. Here, our query groups the documents based on the email field. Then, for each unique email address, the $addToSet operator creates an array of all the unique batch values associated with that email.

#Usage of $addToSet accumulator operator
db.student.aggregate([
{ $group: {
_id: “$email”,
Batch: { $addToSet: “$batch” }
}}
])
)

The following output represents the set of documents with each document with a unique email address and containing an array of all the unique batch values associated with that email.

4. Group by a field and get the average of the numeric field by using $group stage in MongoDB

After that, we have the $avg accumulator operator of group aggregation in MongoDB. The $avg accumulator operator in the $group calculates the average numeric values in a group. Here, the query of group stage group by the age field using the _id field. After that, the $avg operator is used within the $group stage to calculate the average value of the marks field for each group.

#Usage of $avg accumulator operator
db.student.aggregate([
{ $group: {
_id: “$age”,
avg_sales: { $avg: “$marks” }
}}
])
)

The following output shows the grouped fields and the average marks associated with each group.

5. Group by a field and get the maximum value

Moreover, the $group stage has the $max accumulator operator. The $max accumulator operator returns the maximum value encountered in a group. Here, after grouping by the email field, we have used the $max operator in the $group stage to find the maximum value of the age field for each group.

#Usage of $max accumulator operator
db.student.aggregate([
{ $group: {
_id: “$email”,
max_age: { $max: “$age” }
}}
])
)

The following output displayed the value of the maximum age for each of the groups below.

6. Group by a field and get the minimum value

Similar to the above example, we have the $min accumulator to get the minimum value encountered in a group. Here, we have the query of $group stage which is called the $min operator to get the minimum marks for each of the group by name field documents.

#Usage of $min accumulator
db.student.aggregate([
{ $group: {
_id: “$name”,
min_marks: { $min: “$marks” }
}}
])
)

The following output yielded has generated the minimum marks for the groups.

7. Group by a field and get the first value in a group

Last, the $first operator is also an accumulator operator of the $group stage that returns the first value encountered in a group.

#Usage of $first operator
db.student.aggregate(
[
{ $sort: { name: 1, date: 1 } },
{
$group:
{
_id: “$email”,
firstAdmissionDate: { $first: “$date” }
}
}
]
)

Here, we have first used the $sort operator to sort the documents by their name field in ascending order, and then by their date field in ascending order. It then groups the sorted documents by their email field. After that, we use the $first operator in the $group stage which finds the first value of the date field for each group. The following output represents the first value of the date field for all documents with that email address.

If you want to know about grouping values by multiple fields in MongoDB, you can visit the following link.

8. Conclusion

In conclusion, the $group stage in MongoDB is very handful to perform complex data manipulations and analysis like aggregations. All the above examples of the $group stage are easy to implement for grouping the fields and getting the aggregate results.

More details about this topic can be found here.

 The $group stage in MongoDB is used for grouping documents based on a specified key(s) in aggregation. The $group stage takes one or more fields as input and returns a new set of documents that have been grouped based on those fields. In this article, we will discuss the $group stage using different examples with  Read More MongoDB 

Leave a Reply

Your email address will not be published. Required fields are marked *