- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to aggregate sum in MongoDB to get the total count?
To aggregate sum in MongoDB to get the total count, you can use the $sum operator. To understand the above concept, let us create a collection with the document −
> 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") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.aggregateSumDemo.find().pretty();
The following is the output −
{ "_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 }
Here is the query to get the total count.
Case 1 − The query is as follows −
> db.aggregateSumDemo.aggregate([ { ... $group: { ... _id: null, ... "TotalCount": { ... $sum:1 ... } ... } ... } ] );
The following is the output −
{ "_id" : null, "TotalCount" : 4 }
Here is the query to aggregate sum in MongoDB to get the total sum.
Case 2 − The query is as follows −
> db.aggregateSumDemo.aggregate([ { ... $group: { ... _id: null, ... "TotalAmount": { ... $sum: "$Amount" ... } ... } ... } ] );
The following is the output −
{ "_id" : null, "TotalAmount" : 1100 }
- Related Articles
- How to calculate sum in MongoDB with aggregate()?
- MongoDB aggregate to get the count of field values of corresponding duplicate names?
- MongoDB aggregate to get the Mean daily average count of recorded documents in a collection?
- How to get only values in arrays with MongoDB aggregate?
- Get substring in MongoDB aggregate
- MongoDB aggregate $slice to get the length of the array
- MongoDB $unwind to get the count
- How to update after aggregate in MongoDB?
- How to aggregate array documents in MongoDB?
- How to use MongoDB Aggregate to sort?
- Get the average of marks in MongoDB with aggregate?
- Aggregate based on array value to sum values in different MongoDB documents?
- Using MongoDB Aggregate and GroupBy to get the frequency of name record
- Get a count of total documents with MongoDB while using limit?
- How can I aggregate collection and group by field count in MongoDB?

Advertisements