
- 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 sum every field in a sub document of MongoDB?
To sum every field in a sub document, use aggregate framework. Let us first create a collection with documents −
> db.sumEveryFieldDemo.insertOne( ... { ... "_id":101, ... "PlayerDetails": [ ... {"PlayerName":"John","PlayerScore":1000}, ... {"PlayerName":"Carol","PlayerScore":2000}, ... {"PlayerName":"Sam","PlayerScore":3000} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : 101 }
Following is the query to display all documents from a collection with the help of find() method −
> db.sumEveryFieldDemo.find().pretty();
This will produce the following output −
{ "_id" : 101, "PlayerDetails" : [ { "PlayerName" : "John", "PlayerScore" : 1000 }, { "PlayerName" : "Carol", "PlayerScore" : 2000 }, { "PlayerName" : "Sam", "PlayerScore" : 3000 } ] }
Following is the query to sum every field in a sub document of MongoDB −
> db.sumEveryFieldDemo.aggregate( [ ... { $unwind: "$PlayerDetails" }, ... { $group: { ... _id: '$_id', ... sum: { $sum: '$PlayerDetails.PlayerScore' } ... } } ... ] ).pretty();
This will produce the following output −
{ "_id" : 101, "sum" : 6000 }
- Related Articles
- How to add an extra field in a sub document in MongoDB?
- How to get distinct list of sub-document field values in MongoDB?
- How to add a sub-document to sub-document array in MongoDB?
- Add new field to every document in a MongoDB collection?
- Sum MongoDB Sub-documents field?
- Filter sub documents by sub document in MongoDB?
- Search a sub-field on MongoDB?
- How to remove a field completely from a MongoDB document?
- MongoDB query by sub-field?
- How to find a document by the non-existence of a field in MongoDB?
- Find the MongoDB document from sub array?
- Working with MongoDB $sort for sub array document
- How to select MongoDB document that does not consist a specific field?
- Increment a field in MongoDB document which is embedded?
- How to subtract values (TotalPrice – Discount) from document field values in MongoDB?

Advertisements