
- 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
Sum with MongoDB group by multiple columns to calculate total marks with duplicate ids
For this, use aggregate() along with $group. Let us create a collection with documents −
> db.demo627.insertOne({id:101,"Name":"Chris","Marks":54}); { "acknowledged" : true, "insertedId" : ObjectId("5e9acb306c954c74be91e6b2") } > db.demo627.insertOne({id:102,"Name":"Bob","Marks":74}); { "acknowledged" : true, "insertedId" : ObjectId("5e9acb3c6c954c74be91e6b3") } > db.demo627.insertOne({id:101,"Name":"Chris","Marks":87}); { "acknowledged" : true, "insertedId" : ObjectId("5e9acb426c954c74be91e6b4") } > db.demo627.insertOne({id:102,"Name":"Mike","Marks":70}); { "acknowledged" : true, "insertedId" : ObjectId("5e9acb4b6c954c74be91e6b5") }
Display all documents from a collection with the help of find() method −
> db.demo627.find();
This will produce the following output −
{ "_id" : ObjectId("5e9acb306c954c74be91e6b2"), "id" : 101, "Name" : "Chris", "Marks" : 54 } { "_id" : ObjectId("5e9acb3c6c954c74be91e6b3"), "id" : 102, "Name" : "Bob", "Marks" : 74 } { "_id" : ObjectId("5e9acb426c954c74be91e6b4"), "id" : 101, "Name" : "Chris", "Marks" : 87 } { "_id" : ObjectId("5e9acb4b6c954c74be91e6b5"), "id" : 102, "Name" : "Mike", "Marks" : 70 }
Following is the query to sum with MongoDB group by multiple columns to calculate total marks with duplicate ids −
> db.demo627.aggregate([ ... { "$group": { ... "_id": { ... "id" : "$id", .. . "Name":"$Name" ... }, ... "Marks": { "$sum": "$Marks" } ... }}, ... { "$group": { ... "_id": { ... "id" : "$_id.id", ... "Name": "$_id.Name" ... }, ... ... "TotalMarks": { "$sum": "$Marks" } ... }} ... ], { "allowDiskUse": true } ... );
This will produce the following output −
{ "_id" : { "id" : 101, "Name" : "Chris" }, "TotalMarks" : 141 } { "_id" : { "id" : 102, "Name" : "Bob" }, "TotalMarks" : 74 } { "_id" : { "id" : 102, "Name" : "Mike" }, "TotalMarks" : 70 }
- Related Articles
- MongoDB aggregation to sum product price with similar IDs
- MySQL query to GROUP BY multiple columns
- Update table with duplicate ids in MySQL
- How to calculate sum in MongoDB with aggregate()?
- How to delete multiple ids in MongoDB?
- MongoDB query to group duplicate documents
- Count by multiple fields with MongoDB aggregation
- Can we use MySQL GROUP BY clause with multiple columns like MySQL DISTINCT clause is used?
- Retrieving group by result with arrays in MongoDB?
- MySQL multiple COUNT with multiple columns?
- Order MySQL query by multiple ids?
- How to get a rating average in MongoDB based on duplicate ids?
- Using GROUP BY and MAX on multiple columns in MySQL?
- MongoDB Aggregate group multiple result?
- Display the record with non-duplicate Id using MySQL GROUP BY and HAVING

Advertisements