
- 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 get array from a MongoDB collection?
You can use aggregate framework. Let us first create a collection with documents −
> db.getArrayDemo.insertOne( { "CustomerId":101, "CustomerDetails":[ { "CustomerName":"Larry", "CustomerFriendDetails":[ { "CustomerFriendName":"Sam" }, { "CustomerFriendName":"Robert" } ] }, { "CustomerName":"Chris", "CustomerFriendDetails":[ { "CustomerFriendName":"David" }, { "CustomerFriendName":"Carol" } ] } ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cda4949b50a6c6dd317adb7") }
Following is the query to display all documents from a collection with the help of find() method −
> db.getArrayDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cda4949b50a6c6dd317adb7"), "CustomerId" : 101, "CustomerDetails" : [ { "CustomerName" : "Larry", "CustomerFriendDetails" : [ { "CustomerFriendName" : "Sam" }, { "CustomerFriendName" : "Robert" } ] }, { "CustomerName" : "Chris", "CustomerFriendDetails" : [ { "CustomerFriendName" : "David" }, { "CustomerFriendName" : "Carol" } ] } ] }
Following is the query to get array from MongoDB collection −
> db.getArrayDemo.aggregate([ { "$unwind": "$CustomerDetails" }, { "$unwind": "$CustomerDetails.CustomerFriendDetails"}, { "$group": { "_id": null, "CustomerFriendDetails": { "$push": "$CustomerDetails.CustomerFriendDetails.CustomerFriendName" } } } ]);
This will produce the following output −
{ "_id" : null, "CustomerFriendDetails" : [ "Sam", "Robert", "David", "Carol" ] }
- Related Articles
- How to get unique values from MongoDB collection?
- MongoDB query to pull array element from a collection?
- Removing an array element from a MongoDB collection
- How to get a particular element from MongoDB array?
- MongoDB query to remove entire array from collection?
- Get the top most document from a MongoDB collection
- How to drop a numeric collection from MongoDB?
- How to remove duplicates from MongoDB Collection?
- How to delete documents from a collection in MongoDB?
- How to retrieve documents from a collection in MongoDB?
- How to get latest set of data from a MongoDB collection based on the date records?
- How to get items from an object array in MongoDB?
- Count number of documents from MongoDB collection inside Array?
- Get the maximum mark records from a collection with documents in MongoDB
- How to get the child of a MongoDB collection by the key?

Advertisements