15.1 C
London
Friday, July 5, 2024
HomeMongoDBAggregation in MongoDBMongoDB: How to Group By and Sum

MongoDB: How to Group By and Sum

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following syntax to group by and count in MongoDB:

db.collection.aggregate([
    {$group : {_id:"$field_name1", count:{$sum:"$field_name2"}}}
])

Note that field_name1 is the field you’d like to group by and field_name2 is the field you’d like to sum.

The following examples show how to use this syntax with a collection teams with the following documents:

db.teams.insertOne({team: "Mavs", position: "Guard", points: 31})
db.teams.insertOne({team: "Spurs", position: "Forward", points: 22})
db.teams.insertOne({team: "Rockets", position: "Center", points: 19})
db.teams.insertOne({team: "Warriors", position: "Forward", points: 26})
db.teams.insertOne({team: "Cavs", position: "Guard", points: 33})

Example 1: Group By and Sum

We can use the following code to group by the ‘position’ field and count the sum of points for each position.

db.teams.aggregate([
    {$group : {_id:"$position", count:{$sum:"$points"}}}
])

This returns the following results:

{ _id: 'Forward', count: 48 }
{ _id: 'Guard', count: 64 }
{ _id: 'Center', count: 19 }

This tells us:

  • The players with position ‘Forward’ have a total of 48 points.
  • The players with position ‘Guard’ have a total of 64 points.
  • The players with position ‘Center’ have a total of 19 points.

Example 2: Group By and Sum (Then Sort)

We can use the following code to find the sum of points for each position and automatically sort the results in ascending order:

db.teams.aggregate([
    {$group : {_id:"$position", count:{$sum:"$points"}}},
    {$sort: {count:1}} 
])

This returns the following results:

{ _id: 'Center', count: 19 }
{ _id: 'Forward', count: 48 }
{ _id: 'Guard', count: 64 }

The results are sorted by points in ascending order (smallest to largest).

We can use -1 in the count argument to instead sort the results in descending order:

db.teams.aggregate([
    {$group : {_id:"$position", count:{$sum:"$points"}}},
    {$sort: {count:-1}} 
])

This returns the following results:

{ _id: 'Guard', count: 64 }
{ _id: 'Forward', count: 48 }
{ _id: 'Center', count: 19 }

Notice that the results are sorted by points in descending order (largest to smallest).

Note: You can find the complete documentation for $group here.

Additional Resources

The following tutorials explain how to perform other common operations in MongoDB:

MongoDB: How to Count Distinct Values in Field
MongoDB: How to Group By Multiple Fields
MongoDB: How to Group By and Count

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories