How can I calculate the sum of a field in MongoDB?

How can I calculate the sum of a field in MongoDB?

MongoDB is a flexible and powerful database management system that allows users to store and manipulate large amounts of data. One common task in MongoDB is finding the sum of a specific field in a collection. To calculate the sum of a field, one can use the aggregation framework which provides a way to process and analyze data in MongoDB. The $sum operator can be used within the aggregation pipeline to add up the values of a specific field and return the total sum. This process can be further refined by using other operators and functions to filter and group the data before calculating the sum. By utilizing the aggregation framework, users can easily and efficiently calculate the sum of a field in MongoDB.

MongoDB: Calculate the Sum of a Field


You can use the following methods to calculate the sum of values in a field in MongoDB:

Method 1: Calculate Sum of Field

db.collection.aggregate([{$group: {_id:null, sum_val:{$sum:"$valueField"}}}])

Method 2: Calculate Sum of Field by Group

db.collection.aggregate([{$group: {_id:"$groupField", sum_val:{$sum:"$valueField"}}}])

The following examples show how to use each method with a collection teams with the following documents:

db.teams.insertOne({team: "Mavs", points: 30, rebounds: 8})
db.teams.insertOne({team: "Mavs", points: 30, rebounds: 12})
db.teams.insertOne({team: "Spurs", points: 20, rebounds: 7})
db.teams.insertOne({team: "Spurs", points: 25, rebounds: 5})
db.teams.insertOne({team: "Spurs", points: 25, rebounds: 9})

Example 1: Calculate Sum of Field

We can use the following code to calculate the sum of values in the points field:

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

This query returns the following results:

{ _id: null, sum_val: 130} 

From the results we can see that the sum of values in the points field is 130.

We can manually verify this is correct by calculating the sum of the points values by hand:

Sum of Points: 30 + 30 + 20 + 25 + 25 = 130.

Example 2: Calculate Sum of Field by Group

We can use the following code to calculate the sum of the values in the points field, grouped by the team field:

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

This query returns the following results:

{ _id: 'Spurs', sum_val: 60 }
{ _id: 'Mavs', sum_val: 70 } 

From the results we can see:

  • The sum of points for the Spurs is 60.
  • The sum of points for the Mavs is 70.

Note: You can find the complete documentation for the $sum function .

Additional Resources

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

Cite this article

stats writer (2024). How can I calculate the sum of a field in MongoDB?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-calculate-the-sum-of-a-field-in-mongodb/

stats writer. "How can I calculate the sum of a field in MongoDB?." PSYCHOLOGICAL SCALES, 30 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-calculate-the-sum-of-a-field-in-mongodb/.

stats writer. "How can I calculate the sum of a field in MongoDB?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-calculate-the-sum-of-a-field-in-mongodb/.

stats writer (2024) 'How can I calculate the sum of a field in MongoDB?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-calculate-the-sum-of-a-field-in-mongodb/.

[1] stats writer, "How can I calculate the sum of a field in MongoDB?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I calculate the sum of a field in MongoDB?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top