• Post author:
  • Post category:MongoDB
  • Post last modified:May 9, 2024
  • Reading time:8 mins read
You are currently viewing MongoDB Group by Multiple Fields

How do group values by multiple fields in MongoDB? To group values by multiple fields, you need to use the $group stage of the aggregation pipeline. The $group stage in MongoDB is one of the most commonly used stages in the pipeline for grouping data based on one or more fields. The collection used over here is student with these documents.

Advertisements

# Create Collection
db.student.insertMany([
    {   
        _id: 1,
        firstName: "Candice",
        lastName: "Mark",
        age: 22,
        projectMarks: 99,
        examsMarks: 70,
        assignmentMarks: 10,
        status:[{
        "course": "Python", "batch": 2023}]
    },
    {   
         _id: 2,
        firstName: "Dave",
        lastName: "James",
        age: 21,
        projectMarks: 99,
        examsMarks: 70,
        assignmentMarks: 10,
        status:[{
        "course": "MongoDB", "batch": 2023}]
    },
    {   
        _id: 3,
        firstName: "Ivan",
        lastName: "Seth",
        age: 24,
        projectMarks: 99,
        examsMarks: 70,
        assignmentMarks: 10,
        status:[{
        "course": "Java", "batch": 2022}]
    }
])

1. Group by a Single Field in MongoDB

To group by a single field in MongoDB, use the aggregate() function along with $group stage. The $group stage groups a single field for multiple documents.

Consider the following aggregation pipeline. The $group stage groups the documents by the age field. Then it creates a new field called marks using the $sum operator to count the number of orders for each unique status value. Here, the _id field specified the field age to group by and the $sum operator adds up the values of the specified field marks for all documents in the group.


# Usage of $group stage to group a single field
db.student.aggregate([
  { $group: { _id: "$age", marks: { $sum: 1 } } }
])

The output gives the age value as the _id field and the student marks for that age as the marks field for each document.

MongoDB Group Values

2. Group by Multiple Fields in MongoDB

Alternatively, you can also use the aggregate() function along with $group stage to group by multiple columns in MongoDB.

Consider the following example. The $group operator groups the documents by the firstName and lastName fields. Also, it generates a new field called _id with a nested object containing both firstName and lastName fields to group by the unique combinations of these fields. Then, it creates a new field called age using the $sum operator to count the number of documents in each group.


# Usage of $group stage to group a multiple fields
db.student.aggregate([
  { $group: { _id: { firstName: "$firstName", lastName: "$lastName" },
             age: { $sum: 1 } 
            } 
   }
])

The output here shows the group aggregation results as the _id.firstName and _id.lastName fields, respectively. And the count of documents for that combination as the age field.

3. Group by the Multiple Expressions

Next, we can use multiple expressions to group values in MongoDB.

The $group stage is called in the aggregate method to group the documents by the projectMarks field. After that, it creates a new field called _id with a nested field called projectMarks to group by the unique values of projectMarks. It also creates two new fields called total_examsMarks and total_assignmentMarks using the $sum operator to calculate the total marks for exams and assignments, respectively.

Further, the $sum operator is used twice to calculate the total marks for exams and assignments.


$ Usage of multiple expressions to group values
db.student.aggregate([
  { $group: { _id: { projectMarks: "$projectMarks" }, 
         total_examsMarks: { $sum: "$examsMarks" }, 
         total_assignmentMarks: { $sum: "$assignmentMarks" } 
         } 
   }
])

The output yielded shows that each document contains the unique value of projectMarks as the _id.projectMarks field and the total marks for exams and assignments.

MongoDB Group by multiple fields

4. Group by the Conditional Statements in MongoDB

Moreover, we can group the values with the $group stage by specifying the conditional statements in MongoDB.

In the following example, we have first called the $match stage which filters the documents in the collection to only include those where the projectMarks field is equal to 99. Then, we have the $group operator which groups the matched documents by the age field and creates a new field called assignmentMarks using the $sum operator to count the number of documents in each group.


#Usage of $group stage by specifying the conditional statements
db.student.aggregate([
  { $match: { projectMarks: { $eq: 99 } } },
  { $group: { _id: "$age", assignmentMarks: { $sum: 1 } } }
])

The output displays the unique age value as the _id field and the count of documents.  

5. Group by a Nested Field in MongoDB

Furthermore, multiple fields can also be grouped if they contain the array.

Consider the following aggregation query. Here the $unwind is called first to deconstruct the status array field in each document and generate a new document for each element in the array.

After that, we have the $group operator which combines the documents by the status.course field. And creates a new field called _id with the value of status.course to group by the unique values of status.course. It also creates a new field called Batch_No using the $sum operator. It helps to calculate the total batch numbers for each unique value of status.course.


#Group by a nested field
db.student.aggregate([
  { $unwind: "$status" },
  { $group: { _id: "$status.course", Batch_No: { $sum: "$status.batch" } } }
])

The output shows that the documents contain the unique value of status.course as the _id field and the total batch numbers.

MongoDB Group by multiple fields

6. Conclusion

In this article, you have learned how to group values by multiple fields, the MongoDB aggregation $group stage allows us to group documents based on multiple fields and perform various operations on the grouped data. We can obtain useful insights from the data if we can master the steps of the aggregation pipeline.