
- 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
âStructuredâ grouping query in MongoDB to display result with a new field displaying the count
For this, use $group in MongoDB IN aggregate(). The $group groups input documents by the specified _id expression and for each distinct grouping, outputs a document. Let us first create a collection with documents −
> db.demo534.insertOne({_id:10,"ProductId":100,"ProductName":"Product-1"}); { "acknowledged" : true, "insertedId" : 10 } > db.demo534.insertOne({_id:11,"ProductId":100,"ProductName":"Product-2"}); { "acknowledged" : true, "insertedId" : 11 } > db.demo534.insertOne({_id:12,"ProductId":101,"ProductName":"Product-1"}); { "acknowledged" : true, "insertedId" : 12 }
Display all documents from a collection with the help of find() method −
> db.demo534.find();
This will produce the following output −
{ "_id" : 10, "ProductId" : 100, "ProductName" : "Product-1" } { "_id" : 11, "ProductId" : 100, "ProductName" : "Product-2" } { "_id" : 12, "ProductId" : 101, "ProductName" : "Product-1" }
Following is the query to implement structured grouping query in MongoDB −
> db.demo534.aggregate( ... { ... $group: ... { ... _id: ... { ... productName: "$ProductName", ... productId: "$ProductId" ... }, ... count: ... { ... $sum: 1 ... } ... }, ... }, ... { ... $group: ... { ... _id: "$_id.productId", ... itemCounts: ... { ... "$push": ... { ... productName: "$_id.productName", ... count: "$count" ... } ... } ... } ... })
This will produce the following output −
{ "_id" : 101, "itemCounts" : [ { "productName" : "Product-1", "count" : 1 } ] } { "_id" : 100, "itemCounts" : [ { "productName" : "Product-2", "count" : 1 }, { "productName" : "Product-1", "count" : 1 } ] }
- Related Articles
- MongoDB query to count the number of array items in documents and display in a new field
- MongoDB query to create new field and count set the count of another field in it?
- MySQL query to count occurrences of distinct values and display the result in a new column?
- MongoDB query to add a new field and concatenate the price result divided by a specific number in it
- How to project grouping into object in MongoDB and display only the marks field?
- MySQL query to group by names and display the count in a new column
- MySQL query to count the duplicate ID values and display the result in a separate column
- MongoDB query to display alternative documents with mapReduce() function and emit even field values
- MongoDB query select and display only a specific field from the document?
- MySQL query to get the length of all columns and display the result in a single new column?
- MongoDB query to update array with another field?
- Query to divide the values of two columns and display the result in a new column using MySQL wildcard?
- Particular field as result in MongoDB?
- MySQL query to get the count of values and display the count in a new column ordered in descending order\n
- Count number of occurrences of records in a MySQL table and display the result in a new column?

Advertisements