
- 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 items from an object array in MongoDB?
To get items from an object array, use aggregate(). Let us create a collection with documents −
> db.demo459.insertOne( ... { "_id" : 1, ... "Information" : [ ... { ... "Name" : "Chris", ... "_id" : new ObjectId(), ... "details" : [ ... "HR" ... ] ... }, ... { ... ... "Name" : "David", ... "_id" : new ObjectId(), ... "details" : [ ... "Developer" ... ] ... }, ... { ... ... "Name" : "Bob", ... "_id" : new ObjectId(), ... "details" : [ ... "Account" ... ] ... } ... ] ... } ... ) { "acknowledged" : true, "insertedId" : 1 }
Display all documents from a collection with the help of find() method −
> db.demo459.find();
This will produce the following output −
{ "_id" : 1, "Information" : [ { "Name" : "Chris", "_id" : ObjectId("5e7ef4a7dbcb9adb296c95c9"), "details" : [ "HR" ] }, { "Name" : "David", "_id" : ObjectId("5e7ef4a7dbcb9adb296c95ca"), "details" : [ "Developer" ] }, { "Name" : "Bob", "_id" : ObjectId("5e7ef4a7dbcb9adb296c95cb"), "details" : [ "Account" ] } ] }
Following is the query to get items from an object array in MongoDB −
> db.demo459.aggregate([ ... { $unwind: '$Information' }, ... { $unwind: '$Information.details' }, ... { $match: { 'Information.Name': { $in: ["Chris","Bob"]} } }, ... { $group: { _id: null, detailList: { $addToSet: '$Information.details' } } }, ... ])
This will produce the following output −
{ "_id" : null, "detailList" : [ "Account", "HR" ] }
- Related Articles
- How to push new items to an array inside of an object in MongoDB?
- How to remove object from array in MongoDB?
- Get array items inside a MongoDB document?
- How to get items from a series object using the get() method?
- MongoDB query to find data from an array inside an object?
- How to get a specific object from array of objects inside specific MongoDB document?
- How to get array from a MongoDB collection?
- How to count items in array with MongoDB?
- Query to retrieve multiple items in an array in MongoDB?
- Querying from part of object in an array with MongoDB
- Remove object from array in MongoDB?
- MongoDB query to access an object in an array
- How to get a particular element from MongoDB array?
- How to get items with a specific value from documents using MongoDB shell?
- Get attribute list from MongoDB object?

Advertisements