- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Make an array of multiple arrays in MongoDB?
To make an array of multiple arrays, use $unwind in MongoDB aggregate. Let us create a collection with documents −
> db.demo289.insertOne({"Section":["A","B","E"],"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4c06fcf49383b52759cbc3") } > db.demo289.insertOne({"Section":["C","D","B"],"Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4c070af49383b52759cbc4") }
Display all documents from a collection with the help of find() method −
> db.demo289.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e4c06fcf49383b52759cbc3"), "Section" : [ "A", "B", "E" ], "Name" : "Chris" } { "_id" : ObjectId("5e4c070af49383b52759cbc4"), "Section" : [ "C", "D", "B" ], "Name" : "David" }
Following is the query to make a single array of multiple arrays in MongoDB −
> db.demo289.aggregate({ $unwind : "$Section" } );
This will produce the following output −
{ "_id" : ObjectId("5e4c06fcf49383b52759cbc3"), "Section" : "A", "Name" : "Chris" } { "_id" : ObjectId("5e4c06fcf49383b52759cbc3"), "Section" : "B", "Name" : "Chris" } { "_id" : ObjectId("5e4c06fcf49383b52759cbc3"), "Section" : "E", "Name" : "Chris" } { "_id" : ObjectId("5e4c070af49383b52759cbc4"), "Section" : "C", "Name" : "David" } { "_id" : ObjectId("5e4c070af49383b52759cbc4"), "Section" : "D", "Name" : "David" } { "_id" : ObjectId("5e4c070af49383b52759cbc4"), "Section" : "B", "Name" : "David" }
- Related Articles
- Querying an array of arrays in MongoDB?
- Aggregate multiple arrays into one huge array with MongoDB?
- Update multiple elements in an array in MongoDB?
- Pull multiple objects from an array in MongoDB?
- Match multiple criteria inside an array with MongoDB?
- Query to retrieve multiple items in an array in MongoDB?
- What kind of MongoDB query finds same value multiple times in an array?
- Which MongoDB query finds same value multiple times in an array?
- How to match multiple criteria inside an array with MongoDB?
- How can we make an Array of Objects from n properties of n arrays in JavaScript?
- MongoDB find by multiple array items?
- Function to flatten array of multiple nested arrays without recursion in JavaScript
- MongoDB find by multiple array items using $in?
- Perform multiple updates with bulk operations and update elements in an array in MongoDB
- How to get single array from multiple arrays in JavaScript

Advertisements