
- 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
MongoDB aggregate to convert multiple documents into single document with an array?
For aggregate in MongoDB, use aggregate(). Let us create a collection with documents −
> db.demo248.insertOne({"id":101,"Name":"Chris","Age":21,"CountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b6651627c0c63e7dba6d") } > db.demo248.insertOne({"id":101,"Name":"Bob","Age":22,"CountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b6741627c0c63e7dba6e") } > db.demo248.insertOne({"id":102,"Name":"Mike","Age":20,"CountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b6811627c0c63e7dba6f") }
Display all documents from a collection with the help of find() method −
> db.demo248.find();
This will produce the following output −
{ "_id" : ObjectId("5e46b6651627c0c63e7dba6d"), "id" : 101, "Name" : "Chris", "Age" : 21, "CountryName" : "US" } { "_id" : ObjectId("5e46b6741627c0c63e7dba6e"), "id" : 101, "Name" : "Bob", "Age" : 22, "CountryName" : "UK" } { "_id" : ObjectId("5e46b6811627c0c63e7dba6f"), "id" : 102, "Name" : "Mike", "Age" : 20, "CountryName" : "AUS" }
Following is the query to convert multiple documents into single document with an array −
> db.demo248.aggregate([ ... { ... $group : { ... _id : "$id", ... details : { ... $push : { ... id:"$id", ... Name:"$Name", ... Age:"$Age", ... CountryName:"$CountryName" ... } ... } ... } ... } ...])
This will produce the following output −
{ "_id" : 102, "details" : [ { "id" : 102, "Name" : "Mike", "Age" : 20, "CountryName" : "AUS" } ] } { "_id" : 101, "details" : [ { "id" : 101, "Name" : "Chris", "Age" : 21, "CountryName" : "US" }, { "id" : 101, "Name" : "Bob", "Age" : 22, "CountryName" : "UK" } ] }
- Related Articles
- Aggregate multiple arrays into one huge array with MongoDB?
- How to aggregate array documents in MongoDB?
- MongoDB query to push document into an array
- Aggregate by country, state and city in a MongoDB collection with multiple documents
- MongoDB query to convert an array to a map of documents with n attributes?
- MongoDB Aggregate to get average from document and of array elements?
- Aggregate based on array value to sum values in different MongoDB documents?
- How to remove all documents from a collection except a single document in MongoDB?
- MongoDB Aggregate group multiple result?
- MongoDB Convert One record with an array to multiple records in a new collection?
- How to insert multiple document into a MongoDB collection using Java?
- How to convert multiple columns into single column in an R data frame?
- MongoDB query to find documents having two values in an array conforming to multiple criteria?
- MongoDB query to find matching documents given an array with values?
- MongoDB query to add multiple documents

Advertisements