
- 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 product price with similar IDs
You need to use $group to group documents with specified _id expression. Let us first create a collection with documents −
> db.aggreagateDemo.insertOne({"Product_Id":1,"ProductPrice":50}); { "acknowledged" : true, "insertedId" : ObjectId("5e06d3c025ddae1f53b621d9") } > db.aggreagateDemo.insertOne({"Product_Id":2,"ProductPrice":100}); { "acknowledged" : true, "insertedId" : ObjectId("5e06d3c625ddae1f53b621da") } > db.aggreagateDemo.insertOne({"Product_Id":2,"ProductPrice":500}); { "acknowledged" : true, "insertedId" : ObjectId("5e06d3cb25ddae1f53b621db") } > db.aggreagateDemo.insertOne({"Product_Id":1,"ProductPrice":150}); { "acknowledged" : true, "insertedId" : ObjectId("5e06d3d125ddae1f53b621dc") }
Following is the query to display all documents from a collection with the help of find() method −
> db.aggreagateDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e06d3c025ddae1f53b621d9"), "Product_Id" : 1, "ProductPrice" : 50 } { "_id" : ObjectId("5e06d3c625ddae1f53b621da"), "Product_Id" : 2, "ProductPrice" : 100 } { "_id" : ObjectId("5e06d3cb25ddae1f53b621db"), "Product_Id" : 2, "ProductPrice" : 500 } { "_id" : ObjectId("5e06d3d125ddae1f53b621dc"), "Product_Id" : 1, "ProductPrice" : 150 }
Here is the query to perform aggregation and group with id −
> db.aggreagateDemo.aggregate([ ... { ... $group: { ... _id: "$Product_Id", ... TotalValue:{$sum: "$ProductPrice"} ... } ... } ... ] ... );
This will produce the following output −
{ "_id" : 2, "TotalValue" : 600 } { "_id" : 1, "TotalValue" : 200 }
- Related Articles
- MongoDB aggregation of elements with similar ids in different documents?
- Find average of corresponding records (Product Price) from duplicate product ids in MYSQL
- Sum with MongoDB group by multiple columns to calculate total marks with duplicate ids
- MongoDB aggregation with multiple keys
- How to use $ifNull with MongoDB aggregation?
- MongoDB query to replace value with aggregation?
- Grouping the array items in MongoDB and get the count the products with similar price?
- Perform min/max with MongoDB aggregation
- MongoDB aggregation with equality inside array?
- MySQL query to sum the Product Price values from similar columns for same customers and display the result in the same column
- How to calculate a sum of specific documents using MongoDB aggregation?
- MongoDB aggregation / math operation to sum score of a specific student
- MongoDB aggregation to fetch documents with specific field value?
- Get Absolute value with MongoDB aggregation framework?
- MongoDB aggregation framework with group query example?

Advertisements