
- 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 a specific object from array of objects inside specific MongoDB document?
To get specific object from array of objects, use positional operator($). Let us first create a collection with documents −
> db.getASpecificObjectDemo.insertOne( ... { ... _id :1,f ... "CustomerName" : "Larry", ... "CustomerDetails" : { ... "CustomerPurchaseDescription": [{ ... id :100, ... "ProductName" : "Product-1", ... "Amount":10000 ... },{ ... id :101, ... "ProductName" : "Product-2", ... "Amount":10500 ... }, ... { ... id :102, ... "ProductName" : "Product-3", ... "Amount":10200 ... } ... ] ... } ... } ... ); { "acknowledged" : true, "insertedId" : 1 }
Following is the query to display all documents from a collection with the help of find() method −
> db.getASpecificObjectDemo.find().pretty();
This will produce the following output −
{ "_id" : 1, "CustomerName" : "Larry", "CustomerDetails" : { "CustomerPurchaseDescription" : [ { "id" : 100, "ProductName" : "Product-1", "Amount" : 10000 }, { "id" : 101, "ProductName" : "Product-2", "Amount" : 10500 }, { "id" : 102, "ProductName" : "Product-3", "Amount" : 10200 } ] } }
Following is the query to get a specific object from array of objects inside specific MongoDB document −
> db.getASpecificObjectDemo.find({_id:1, "CustomerDetails.CustomerPurchaseDescription.id":101},{_id:0, "CustomerDetails.CustomerPurchaseDescription.$":1});
This will produce the following output −
{ "CustomerDetails" : { "CustomerPurchaseDescription" : [ { "id" : 101, "ProductName" : "Product-2", "Amount" : 10500 } ] } }
- Related Articles
- How to project specific fields from a document inside an array in Mongodb?
- Filter specific values from a MongoDB document
- Get array items inside a MongoDB document?
- Get specific elements from embedded array in MongoDB?
- MongoDB query to update a specific document from a collection
- MongoDB query to pull a specific value from a document
- Fetch a specific document in MongoDB with array elements
- MongoDB query to remove a specific document
- Return specific MongoDB embedded document
- How to get the matching document inside an array in MongoDB?
- Get count of array elements from a specific field in MongoDB documents?
- Accessing array values in a MongoDB collection to fetch a specific document
- How to remove a specific element from array in MongoDB?
- How to add a field with specific datatype (list, object) in an existing MongoDB document?
- Find document with array that contains a specific value in MongoDB

Advertisements