

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- MongoDB aggregation of elements with similar ids in different documents?
- Find average of corresponding records (Product Price) from duplicate product ids in MYSQL
- Grouping the array items in MongoDB and get the count the products with similar price?
- Sum with MongoDB group by multiple columns to calculate total marks with duplicate ids
- MongoDB aggregation with multiple keys
- MySQL query to sum the Product Price values from similar columns for same customers and display the result in the same column
- MongoDB query to replace value with aggregation?
- How to use $ifNull with MongoDB aggregation?
- Perform min/max with MongoDB aggregation
- MongoDB aggregation with equality inside array?
- How to calculate a sum of specific documents using MongoDB aggregation?
- MongoDB aggregation / math operation to sum score of a specific student
- Get Absolute value with MongoDB aggregation framework?
- MongoDB aggregation framework with group query example?
- Count by multiple fields with MongoDB aggregation
Advertisements