

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 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 Questions & Answers
- 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?
- How to calculate sum of string in MongoDB?
- 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 of elements with similar ids in different documents?
- MongoDB query (aggregation framework) to match a specific field value
- MongoDB aggregation to get two documents with the least marks
- Sum MongoDB Sub-documents field?
- Fetch specific multiple documents in MongoDB
- Sort the MongoDB documents in ascending order with aggregation?
- How to calculate sum in MongoDB with aggregate()?
- MongoDB query to display documents with a specific name irrespective of case
Advertisements