- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Using MongoDB nested $group and $sum to get the count of stocks with similar ProductID?
The $group in MongoDB is used to group input documents by the specified _id expression. Let us create a collection with documents −
> db.demo466.insertOne( ... { ... ... "ProductPrice" :150, ... "ProductQuantity" : 1, ... "ProductName" : "Product-1", ... "ActualAmount" :110, ... "ProductProfit" : 40, ... "ProductId" : 1 ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e80477cb0f3fa88e2279066") } > > db.demo466.insertOne( ... { ... ... "ProductPrice" :150, ... "ProductQuantity" : 1, ... "ProductName" : "Product-1", ... "ActualAmount" :110, ... "ProductProfit" : 40, ... "ProductId" : 2 ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e80477db0f3fa88e2279067") } > db.demo466.insertOne( ... { ... ... "ProductPrice" :170, ... "ProductQuantity" : 2, ... "ProductName" : "Product-2", ... "ActualAmount" :130, ... "ProductProfit" : 50, ... "ProductId" : 3 ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e80477eb0f3fa88e2279068") }
Display all documents from a collection with the help of find() method −
> db.demo466.find();
This will produce the following output −
{ "_id" : ObjectId("5e80477cb0f3fa88e2279066"), "ProductPrice" : 150, "ProductQuantity" : 1, "ProductName" : "Product-1", "ActualAmount" : 110, "ProductProfit" : 40, "ProductId" : 1 } { "_id" : ObjectId("5e80477db0f3fa88e2279067"), "ProductPrice" : 150, "ProductQuantity" : 1, "ProductName" : "Product-1", "ActualAmount" : 110, "ProductProfit" : 40, "ProductId" : 2 } { "_id" : ObjectId("5e80477eb0f3fa88e2279068"), "ProductPrice" : 170, "ProductQuantity" : 2, "ProductName" : "Product-2", "ActualAmount" : 130, "ProductProfit" : 50, "ProductId" : 3 }
Following is the query to use nested $group and $sum in MongoDB −
> db.demo466.aggregate([ ... { ... '$group': { ... '_id': { ... 'ProductName': '$ProductName', ... }, ... 'ActualAmount': {'$sum': '$ActualAmount'}, ... 'ProductQuantity': {'$sum': '$ProductQuantity'}, ... 'ProductId': {'$addToSet': '$ProductId'}, ... }, ... }, ... { ... '$project': { ... 'ProductQuantity': true, ... 'ActualAmount': true, ... 'NumberOfProductInStock': {'$size': '$ProductId'} ... } ... }])
This will produce the following output −
{ "_id" : { "ProductName" : "Product-2" }, "ActualAmount" : 130, "ProductQuantity" : 2, "NumberOfProductInStock" : 1 } { "_id" : { "ProductName" : "Product-1" }, "ActualAmount" : 220, "ProductQuantity" : 2, "NumberOfProductInStock" : 2 }
Advertisements