
- 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
Aggregate by country, state and city in a MongoDB collection with multiple documents
Aggregation operations group values from multiple documents together, and can perform a variety of operations on the grouped data to return a single result.
To aggregate in MongoDB, use aggregate(). Let us create a collection with documents −
> db.demo620.insertOne({"Country":"IND","City":"Delhi",state:"Delhi"}); { "acknowledged" : true, "insertedId" : ObjectId("5e9a8de96c954c74be91e6a1") } > db.demo620.insertOne({"Country":"IND","City":"Bangalore",state:"Karnataka"}); { "acknowledged" : true, "insertedId" : ObjectId("5e9a8e336c954c74be91e6a3") } > db.demo620.insertOne({"Country":"IND","City":"Mumbai",state:"Maharashtra"}); { "acknowledged" : true, "insertedId" : ObjectId("5e9a8e636c954c74be91e6a4") }
Display all documents from a collection with the help of find() method −
> db.demo620.find();
This will produce the following output −
{ "_id" : ObjectId("5e9a8de96c954c74be91e6a1"), "Country" : "IND", "City" : "Delhi", "state" : "Delhi" } { "_id" : ObjectId("5e9a8e336c954c74be91e6a3"), "Country" : "IND", "City" : "Bangalore", "state" : "Karnataka" } { "_id" : ObjectId("5e9a8e636c954c74be91e6a4"), "Country" : "IND", "City" : "Mumbai", "state" : "Maharashtra" }
Following is the query to aggregate by country, state and city −
> db.demo620.aggregate([ ... { "$group": { ... "_id": { ... "Country": "$Country", ... "state": "$state" ... }, ... "City": { ... "$addToSet": { ... "City": "$City" ... } ... } ... }}, ... { "$group": { ... "_id": "$_id.Country", ... "states": { ... "$addToSet": { ... "state": "$_id.state", ... "City": "$City" ... } ... } ... }} ... ]).pretty();
This will produce the following output −
{ "_id" : "IND", "states" : [ { "state" : "Delhi", "City" : [ { "City" : "Delhi" } ] }, { "state" : "Maharashtra", "City" : [ { "City" : "Mumbai" } ] }, { "state" : "Karnataka", "City" : [ { "City" : "Bangalore" } ] } ] }
- Related Articles
- MongoDB aggregate to convert multiple documents into single document with an array?
- How to Update multiple documents in a MongoDB collection using Java?
- How to find MongoDB documents in a collection with a filter on multiple combined fields?
- MongoDB aggregate to get the Mean daily average count of recorded documents in a collection?
- How can I aggregate collection and group by field count in MongoDB?
- How to aggregate array documents in MongoDB?
- Retrieving specific documents from collection by _id in MongoDB
- Find a value in lowercase from a MongoDB collection with documents
- Find all duplicate documents in a MongoDB collection by a key field?
- Get MongoDB documents with max attribute per group in a collection?
- Get all embedded documents with “isMarried” status in a MongoDB collection
- MongoDB Aggregate group multiple result?
- Aggregate multiple arrays into one huge array with MongoDB?
- How can I aggregate nested documents in MongoDB?\n
- Get the maximum mark records from a collection with documents in MongoDB

Advertisements