
- 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
How to merge array of documents in Mongo DB?
To merge, use aggregate() along with $PUSH. Let us create a collection with documents −
> db.demo64.insertOne({"Subjects":["MySQL","MongoDB","Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e28762bcfb11e5c34d89936") } > db.demo64.insertOne({"Subjects":["Oracle","Spring","Python"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e28763fcfb11e5c34d89937") }
Display all documents from a collection with the help of find() method −
> db.demo64.find();
This will produce the following output −
{ "_id" : ObjectId("5e28762bcfb11e5c34d89936"), "Subjects" : [ "MySQL", "MongoDB", "Java" ] } { "_id" : ObjectId("5e28763fcfb11e5c34d89937"), "Subjects" : [ "Oracle", "Spring", "Python" ] }
Following is the query to merge array of document in MongoDB −
> db.demo64.aggregate([ ... { "$group": { ... "_id": null, ... "Subjects": { "$push": "$Subjects" } ... }}, ... { "$project": { "_id": 0 }} ... ]);
This will produce the following output −
{ "Subjects" : [ [ "MySQL", "MongoDB", "Java" ], [ "Oracle", "Spring", "Python" ] ] }
- Related Articles
- How to merge multiple documents in MongoDB?
- How do I return a document with filtered sub-documents using Mongo?
- Update a single list item of a Mongo DB document and increment it by 1
- How to aggregate array documents in MongoDB?
- How to merge two different array of objects using JavaScript?
- How to search documents through an integer array in MongoDB?
- How to filter documents based on an array in MongoDB?
- How to list all collections in the Mongo shell?
- How to list all databases in the Mongo shell?
- How to list all users in the Mongo shell?
- How to Use lua-mongo library in Lua Programming?
- Merge Sorted Array in Python
- How to find latest entries in array over all MongoDB documents?
- How to merge specific elements inside an array together - JavaScript
- How to use MongoDB $pull to delete documents within an Array?

Advertisements