
- 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
MongoDB aggregation to sum individual properties on an object in an array across documents
For this, use aggregate() in MongoDB. Let us first create a collection with documents −
> db.demo131.insertOne( ... { ... "_id": 101, ... "Details": [ ... { ... "PlayerScore": 500, ... "PlayerName": "Chris" ... }, ... { ... "PlayerScore": 400, ... "PlayerName": "David" ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : 101 } > db.demo131.insertOne( ... { ... "_id": 102, ... "Details": [ ... { ... "PlayerScore": 600, ... "PlayerName": "Chris" ... }, ... { ... "PlayerScore": 200, ... "PlayerName": "David" ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : 102 }
Display all documents from a collection with the help of find() method −
> db.demo131.find();
This will produce the following output −
{ "_id" : 101, "Details" : [ { "PlayerScore" : 500, "PlayerName" : "Chris" }, { "PlayerScore" : 400, "PlayerName" : "David" } ] } { "_id" : 102, "Details" : [ { "PlayerScore" : 600, "PlayerName" : "Chris" }, { "PlayerScore" : 200, "PlayerName" : "David" } ] }
Following is the query to sum individual properties on an object in an array −
> db.demo131.aggregate([ ... { $unwind: "$Details" }, ... { ... $group: { ... _id:"$Details.PlayerName", ... Value:{$sum:"$Details.PlayerScore"} ... } ... }, ... { ... $group: { ... _id: 0, ... Details:{ $push: {Details:"$_id",Value:"$Value"}} ... } ... }, ... { ... $project:{Details:1,_id:0} ... } ])
This will produce the following output −
{ "Details" : [ { "Details" : "David", "Value" : 600 }, { "Details" : "Chris", "Value" : 1100 } ] }
- Related Articles
- How to filter documents based on an array in MongoDB?
- Getting a distinct aggregation of an array field across indexes
- How to calculate a sum of specific documents using MongoDB aggregation?
- Aggregation in MongoDB for nested documents?
- Aggregate based on array value to sum values in different MongoDB documents?
- MongoDB query to access an object in an array
- How to search documents through an integer array in MongoDB?
- Count unique items in array-based fields across all MongoDB documents?
- Querying on an array of objects for specific nested documents with MongoDB?
- Filter the properties of an object based on an array and get the filtered object JavaScript
- MongoDB query to match documents that contain an array field
- How to use MongoDB $pull to delete documents within an Array?
- How to sum the value of a key across all documents in a MongoDB collection?
- Query on the last object of an array with MongoDB
- MongoDB aggregation to fetch documents with specific field value?

Advertisements