
- 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
Find values group by another field in MongoDB?
To group by another field, use $group along with $project. Let us first create a collection with documents −
> db.demo374.insertOne( ... { ... ... "Name" : "Chris", ... "HobbyDetails" : [ ... "Reading Book", ... "Playing Football" ... ], ... "CountryName" : "US" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e5a04402ae06a1609a00b04") } > db.demo374.insertOne( ... { ... ... "Name" : "Chris", ... "HobbyDetails" : [ ... "Browsing Internet", ... "Playing Football" ... ], ... "CountryName" : "US" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e5a04402ae06a1609a00b05") }
Display all documents from a collection with the help of find() method −
> db.demo374.find();
This will produce the following output −
{ "_id" : ObjectId("5e5a04402ae06a1609a00b04"), "Name" : "Chris", "HobbyDetails" : [ "Reading Book", "Playing Football" ], "CountryName" : "US" } { "_id" : ObjectId("5e5a04402ae06a1609a00b05"), "Name" : "Chris", "HobbyDetails" : [ "Browsing Internet", "Playing Football" ], "CountryName" : "US" }
Following is the query to find values group by another field in MongoDB −
> db.demo374.aggregate([ ... {$match : { "CountryName" : "US"}}, ... {$group : {"_id" : "$Name", "HobbyDetails" : {$addToSet : "$HobbyDetails"}}}, ... {$project : {"_id" : 0, "Name" : "$_id", "HobbyDetails" : {$reduce : {input : "$HobbyDetails", initialValue : [], in: { $concatArrays : ["$$value", "$$this"] }}}}} ... ]).pretty()
This will produce the following output −
{ "Name" : "Chris", "HobbyDetails" : [ "Browsing Internet", "Playing Football", "Reading Book", "Playing Football" ] }
- Related Articles
- How can I aggregate collection and group by field count in MongoDB?
- Getting a list of values by using MongoDB $group?
- Update MongoDB field using value of another field?
- Fetch specific field values in MongoDB
- Group by dates in MongoDB?
- MongoDB query for counting number of unique fields grouped by another field?
- Find all the non-distinct values of a field in MongoDB?
- How to compare field values in MongoDB?
- MySQL: update field with Group By?
- Search by specific field in MongoDB
- MongoDB query to update array with another field?
- MongoDB SELECT COUNT GROUP BY?
- MongoDB query by sub-field?
- Group by dates in a MongoDB collection?
- GROUP BY a column in another MySQL table

Advertisements