
- 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
Aggregate based on array value to sum values in different MongoDB documents?
For this, use aggregate() in MongoDB. Let us first create a collection with documents −
> db.demo126.insertOne( ... { ... "StudentDetails" : { ... "Number" : 1, ... "OtherDetails" : [ ... { ... "Name" : "Chris", ... "Score" : 55 ... ... } ... ].. }} ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e304b3068e7f832db1a7f56") } > > > db.demo126.insertOne( ... { ... "StudentDetails" : { ... ... "Number" : 2, ... "OtherDetails" : [ ... { ... "Name" : "Chris", ... "Score" : 35 ... ... }, ... { ... "Name" : "David", ... "Score" : 87 ... } ... ... ] ... }} ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e304b3068e7f832db1a7f57") }
Display all documents from a collection with the help of find() method −
> db.demo126.find();
This will produce the following output −
{ "_id" : ObjectId("5e304b3068e7f832db1a7f56"), "StudentDetails" : { "Number" : 1, "OtherDetails" : [ { "Name" : "Chris", "Score" : 55 } ] } } { "_id" : ObjectId("5e304b3068e7f832db1a7f57"), "StudentDetails" : { "Number" : 2, "OtherDetails" : [ { "Name" : "Chris", "Score" : 35 }, { "Name" : "David", "Score" : 87 } ] } }
Following is the query to aggregate based on array value −
> db.demo126.aggregate([ ... { ... $unwind:"$StudentDetails.OtherDetails" ... }, ... { ... $group:{ ... _id:"$StudentDetails.OtherDetails.Name", ... quantity:{ ... $sum:"$StudentDetails.OtherDetails.Score" ... } ... } ... } ... ])
This will produce the following output −
{ "_id" : "David", "quantity" : 87 } { "_id" : "Chris", "quantity" : 90 }
- Related Articles
- How to aggregate array documents in MongoDB?
- How to filter documents based on an array in MongoDB?
- MongoDB Aggregate sum of values in a list of dictionaries for all documents?
- Querying array of Embedded Documents in MongoDB based on Range?
- MongoDB aggregate to convert multiple documents into single document with an array?
- How to search for documents based on the value of adding two properties in MongoDB?
- MongoDB query to match documents with array values greater than a specific value
- MongoDB aggregation to sum individual properties on an object in an array across documents
- How can I aggregate nested documents in MongoDB?\n
- Sum the score of duplicate column values in MongoDB documents?
- Count unique items in array-based fields across all MongoDB documents?
- How to calculate sum in MongoDB with aggregate()?
- MongoDB query to aggregate nested array
- Match MongoDB documents with fields not containing values in array?
- How can I find documents in MongoDB based on the number of matched objects within an array?

Advertisements