How to Calculate a Sum in MongoDB

Have a Database Problem? Speak with an Expert for Free
Get Started >>

How to use sum(Aggregation) in MongoDB?

In this article, we are going to talk about sum, an aggregation method in databases. With the help of the sum method, you can sum up large sets of numbers and calculate totals that can help in reports and other calculations. Let’s get straight into showing you how to perform a MongoDB sum.

What is the use of Sum in MongoDB and how to use it?

The $sum operator is used to sum up the values of fields in documents.

Today, I’m going to explain this how to perform a sum with an example. So let’s get started without wasting time! First of all, we are going to create a collection with some documents:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
>db.aggregateSumDemo.insertOne({"CustomerName":"Larry","Amount":140});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8baa0680f10143d8431e18")
}
>db.aggregateSumDemo.insertOne({"CustomerName":"Mike","Amount":160});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8baa1380f10143d8431e19")
}
>db.aggregateSumDemo.insertOne({"CustomerName":"Sam","Amount":300});{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8baa1c80f10143d8431e1a")
}
>db.aggregateSumDemo.insertOne({"CustomerName":"David","Amount":500});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8baa2580f10143d8431e1b")
}

If you want to see all the documents that you have added in the collection then you have to use find() method. Here is the query that you would use:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
> db.aggregateSumDemo.find().pretty();
Once you hit enter, it will show up results on as shown below:
{
   "_id" : ObjectId("5c8baa0680f10143d8431e18"),
   "CustomerName" : "Larry",
   "Amount" : 140
}
{
   "_id" : ObjectId("5c8baa1380f10143d8431e19"),
   "CustomerName" : "Mike",
   "Amount" : 160
}
{
   "_id" : ObjectId("5c8baa1c80f10143d8431e1a"),
   "CustomerName" : "Sam",
   "Amount" : 300
}
{
   "_id" : ObjectId("5c8baa2580f10143d8431e1b"),
   "CustomerName" : "David",
   "Amount" : 500
}

To give you a better overview of it we are going to show you two use cases. In case 1, we will sum up the total number of records.

Case 1

Here is the query in MongoDB to get the records count:

1
2
3
4
5
6
7
8
> db.aggregateSumDemo.aggregate([ {
   ... $group: {
      ... _id: null,
      ... "TotalCount": {
         ... $sum:1
      ... }
   ... }
... } ] );

Case 1 Result

1
{ "_id" : null, "TotalCount" : 4 }

Here is the query to aggregate sum in MongoDB to get the total sum. You can see in the results that after running the query we got the total number of available records which is 4. Let’s go with the other example where we sum up the amount:

Case 2 − Here is the query to sum the amount

1
2
3
4
5
6
7
8
> db.aggregateSumDemo.aggregate([ {
   ... $group: {
      ... _id: null,
      ... "TotalAmount": {
         ... $sum: "$Amount"
      ... }
   ... }
... } ] );

Case 2 Result

1
{ "_id" : null, "TotalAmount" : 1100 }

When we sum the amounts of all the records then you can see the total amount as shown above.

Conclusion

I hope you now understand how to use $sum operator in MongoDB. The $sum operator should come in handy in both basic and complex queries. We hope you learned what you needed to apply it to your specific problem. Thanks for your time.

Just the Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
>db.aggregateSumDemo.insertOne({"CustomerName":"Larry","Amount":140});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8baa0680f10143d8431e18")
}
>db.aggregateSumDemo.insertOne({"CustomerName":"Mike","Amount":160});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8baa1380f10143d8431e19")
}
>db.aggregateSumDemo.insertOne({"CustomerName":"Sam","Amount":300});{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8baa1c80f10143d8431e1a")
}
>db.aggregateSumDemo.insertOne({"CustomerName":"David","Amount":500});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8baa2580f10143d8431e1b")
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
> db.aggregateSumDemo.find().pretty();
Once you hit enter, it will show up results on as shown below:
{
   "_id" : ObjectId("5c8baa0680f10143d8431e18"),
   "CustomerName" : "Larry",
   "Amount" : 140
}
{
   "_id" : ObjectId("5c8baa1380f10143d8431e19"),
   "CustomerName" : "Mike",
   "Amount" : 160
}
{
   "_id" : ObjectId("5c8baa1c80f10143d8431e1a"),
   "CustomerName" : "Sam",
   "Amount" : 300
}
{
   "_id" : ObjectId("5c8baa2580f10143d8431e1b"),
   "CustomerName" : "David",
   "Amount" : 500
}
1
2
3
4
5
6
7
8
> db.aggregateSumDemo.aggregate([ {
   ... $group: {
      ... _id: null,
      ... "TotalCount": {
         ... $sum:1
      ... }
   ... }
... } ] );
1
{ "_id" : null, "TotalCount" : 4 }
1
2
3
4
5
6
7
8
> db.aggregateSumDemo.aggregate([ {
   ... $group: {
      ... _id: null,
      ... "TotalAmount": {
         ... $sum: "$Amount"
      ... }
   ... }
... } ] );
1
{ "_id" : null, "TotalAmount" : 1100 }

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.