
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
How to calculate a sum of specific documents using MongoDB aggregation?
To sum, use $sum and to get sum of specific documents, you need to group them using $group in MongoDB.
Let us first create a collection with documents −
>db.calculateSumOfDocument.insertOne({"ListOfUsers":["Carol","Bob"],"UsersDetails":[{"FirstUser":"Carol","TotalLikes":20},{"FirstUser":"Bob","TotalLikes":45}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e084e9125ddae1f53b6220c") } >db.calculateSumOfDocument.insertOne({"ListOfUsers":["Carol","Bob"],"UsersDetails":[{"FirstUser":"Carol","TotalLikes":60},{"FirstUser":"Bob","TotalLikes":50}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e084f6125ddae1f53b6220d") }
Following is the query to display all documents from a collection with the help of find() method −
> db.calculateSumOfDocument.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e084e9125ddae1f53b6220c"), "ListOfUsers" : [ "Carol", "Bob" ], "UsersDetails" : [ { "FirstUser" : "Carol", "TotalLikes" : 20 }, { "FirstUser" : "Bob", "TotalLikes" : 45 } ] } { "_id" : ObjectId("5e084f6125ddae1f53b6220d"), "ListOfUsers" : [ "Carol", "Bob" ], "UsersDetails" : [ { "FirstUser" : "Carol", "TotalLikes" : 60 }, { "FirstUser" : "Bob", "TotalLikes" : 50 } ] }
Following is the query to calculate sum of specific documents using aggregation −
> db.calculateSumOfDocument.aggregate([ ... { ... "$match": { "UsersDetails.FirstUser": "Carol" } ... }, ... { "$unwind" : "$UsersDetails" }, ... { ... "$match": { "UsersDetails.FirstUser": "Carol" } ... }, ... { ... "$group": { ... "_id": null, ... "total": { "$sum": "$UsersDetails.TotalLikes" } ... } ... } ... ]);
This will produce the following output −
{ "_id" : null, "total" : 80 }
- Related Articles
- MongoDB aggregation to fetch documents with specific field value?
- MongoDB aggregation / math operation to sum score of a specific student
- Aggregation in MongoDB for nested documents?
- How to find MongoDB documents with a specific string?
- MongoDB aggregation to sum individual properties on an object in an array across documents
- How to get items with a specific value from documents using MongoDB shell?
- MongoDB aggregation to get two documents with the least marks
- MongoDB aggregation of elements with similar ids in different documents?
- MongoDB query (aggregation framework) to match a specific field value
- Sort the MongoDB documents in ascending order with aggregation?
- Fetch specific multiple documents in MongoDB
- How to calculate sum of string in MongoDB?
- MongoDB query to display documents with a specific name irrespective of case
- Sum MongoDB Sub-documents field?
- How to sort the documents of a MongoDB collection using java?

Advertisements