
- 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
Multilevel $group using MongoDB
To implement multilevel $group, use MongoDB aggregate. Let us create a collection with documents −
> db.demo76.insertOne({ Name:"Chris","Age" : 21, "CountryName" : 'US' }); { "acknowledged" : true, "insertedId" : ObjectId("5e2bd3e571bf0181ecc42281") } > db.demo76.insertOne({ Name:"Chris","Age" : 21, "CountryName" : 'US' }); { "acknowledged" : true, "insertedId" : ObjectId("5e2bd3e571bf0181ecc42282") } > db.demo76.insertOne({ Name:"Chris","Age" : 23, "CountryName" : 'UK' }); { "acknowledged" : true, "insertedId" : ObjectId("5e2bd3e571bf0181ecc42283") } > db.demo76.insertOne({ Name:"Chris","Age" : 23, "CountryName" : 'UK' }); { "acknowledged" : true, "insertedId" : ObjectId("5e2bd3e571bf0181ecc42284") } > db.demo76.insertOne({ Name:"Chris","Age" : 21, "CountryName" : 'AUS' }); { "acknowledged" : true, "insertedId" : ObjectId("5e2bd3e571bf0181ecc42285") } > db.demo76.insertOne({ Name:"Chris","Age" : 21, "CountryName" : 'AUS' }); { "acknowledged" : true, "insertedId" : ObjectId("5e2bd3e571bf0181ecc42286") } > db.demo76.insertOne({ Name:"Chris","Age" : 23, "CountryName" : 'AUS' }); { "acknowledged" : true, "insertedId" : ObjectId("5e2bd3e571bf0181ecc42287") } > db.demo76.insertOne({ Name:"Chris","Age" : 21, "CountryName" : 'UK' }); { "acknowledged" : true, "insertedId" : ObjectId("5e2bd3e571bf0181ecc42288") }
Display all documents from a collection with the help of find() method −
> db.demo76.find();
This will produce the following output −
{ "_id" : ObjectId("5e2bd3e571bf0181ecc42281"), "Name" : "Chris", "Age" : 21, "CountryName" : "US" } { "_id" : ObjectId("5e2bd3e571bf0181ecc42282"), "Name" : "Chris", "Age" : 21, "CountryName" : "US" } { "_id" : ObjectId("5e2bd3e571bf0181ecc42283"), "Name" : "Chris", "Age" : 23, "CountryName" : "UK" } { "_id" : ObjectId("5e2bd3e571bf0181ecc42284"), "Name" : "Chris", "Age" : 23, "CountryName" : "UK" } { "_id" : ObjectId("5e2bd3e571bf0181ecc42285"), "Name" : "Chris", "Age" : 21, "CountryName" : "AUS" } { "_id" : ObjectId("5e2bd3e571bf0181ecc42286"), "Name" : "Chris", "Age" : 21, "CountryName" : "AUS" } { "_id" : ObjectId("5e2bd3e571bf0181ecc42287"), "Name" : "Chris", "Age" : 23, "CountryName" : "AUS" } { "_id" : ObjectId("5e2bd3e571bf0181ecc42288"), "Name" : "Chris", "Age" : 21, "CountryName" : "UK" }
Following is the query to implement multilevel $group −
> db.demo76.aggregate([ ... { "$match": { "Name": "Chris" }}, ... { "$group": { ... "_id": { "Age": "$Age", "CountryName": "$CountryName" }, ... "count": { "$sum": 1 } ... }}, ... { "$group": { ... "_id": "$_id.Age", ... "CountryName": { ... "$push": { ... "k": "$_id.CountryName", ... "v": "$count" ... } ... } ... }}, ... { "$addFields": { ... "CountryName": { ... "$arrayToObject": "$CountryName" ... } ... }} ... ]);
This will produce the following output −
{ "_id" : 23, "CountryName" : { "AUS" : 1, "UK" : 2 } } { "_id" : 21, "CountryName" : { "UK" : 1, "AUS" : 2, "US" : 2 } }
- Related Articles
- MongoDB query to group several fields using aggregation framework?
- Getting a list of values by using MongoDB $group?
- How to create a multilevel listView using Kotlin?
- MongoDB SELECT COUNT GROUP BY?
- Group by dates in MongoDB?
- MongoDB Aggregate group multiple result?
- Multilevel inheritance in Java
- Group across two columns in MongoDB?
- MongoDB query to group duplicate documents
- MongoDB query to group by _id
- C# Example for MultiLevel Inheritance
- MultiLevel Inheritance in Dart Programming
- Implement MongoDB Aggregate - unwind, group and project?
- Group query upon nested object in MongoDB?
- MongoDB aggregation framework with group query example?

Advertisements