
- 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
Retrieving group by result with arrays in MongoDB?
To retrieve group by result with arrays, use aggregate(). We will also use $addToSet operator. It adds a value to an array unless the value is already present, in which case $addToSet does nothing to that array.
Let us create a collection with documents −
> db.demo498.insertOne({id:1,Name:["Chris"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e86192b987b6e0e9d18f553") } > db.demo498.insertOne({id:2,Name:["David"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e86192d987b6e0e9d18f554") } > db.demo498.insertOne({id:3,Name:["Chris"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e861931987b6e0e9d18f555") } > db.demo498.insertOne({id:4,Name:["Bob"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e861942987b6e0e9d18f556") } > db.demo498.insertOne({id:5,Name:["David"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e861947987b6e0e9d18f557") }
Display all documents from a collection with the help of find() method −
> db.demo498.find();
This will produce the following output −
{ "_id" : ObjectId("5e86192b987b6e0e9d18f553"), "id" : 1, "Name" : [ "Chris" ] } { "_id" : ObjectId("5e86192d987b6e0e9d18f554"), "id" : 2, "Name" : [ "David" ] } { "_id" : ObjectId("5e861931987b6e0e9d18f555"), "id" : 3, "Name" : [ "Chris" ] } { "_id" : ObjectId("5e861942987b6e0e9d18f556"), "id" : 4, "Name" : [ "Bob" ] } { "_id" : ObjectId("5e861947987b6e0e9d18f557"), "id" : 5, "Name" : [ "David" ] }
Following is the query to retrieve group by result with arrays in MongoDB −
> db.demo498.aggregate([{ ... $unwind : "$Name" ... }, { ... $group : { ... _id : $quot;$Name", ... Id : { ... $addToSet : "$id" ... } ... } ... } ... ])
This will produce the following output −
{ "_id" : "David", "Id" : [ 5, 2 ] } { "_id" : "Bob", "Id" : [ 4 ] } { "_id" : "Chris", "Id" : [ 3, 1 ] }
- Related Articles
- MongoDB Aggregate group multiple result?
- Retrieving specific documents from collection by _id in MongoDB
- Group by dates in MongoDB?
- MongoDB SELECT COUNT GROUP BY?
- Group by dates in a MongoDB collection?
- MongoDB query to group by _id
- Retrieving the first document in a MongoDB collection?
- Find values group by another field in MongoDB?
- Search for documents with similar arrays in MongoDB and order by similarity value
- Retrieving a Subset of Fields from MongoDB
- Sum with MongoDB group by multiple columns to calculate total marks with duplicate ids
- Retrieving array values from a find query in MongoDB?
- Listing all rows by group with MySQL GROUP BY?
- To Aggregate Totals in One Group with MongoDB
- Group all documents with common fields in MongoDB?

Advertisements