
- 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
Get specific elements from embedded array in MongoDB?
To get specific elements, use $match with dot notation. Let us create a collection with documents −
> db.demo641.insert( ... { ... ProductId:101, ... "ProductInformation": ... ( [ ... { ... ProductName:"Product-1", ... "ProductPrice":1000 ... }, ... { ... ProductName:"Product-2", ... "ProductPrice":500 ... }, ... { ... ProductName:"Product-3", ... "ProductPrice":2000 ... }, ... { ... ProductName:"Product-4", ... "ProductPrice":3000 ... } ... ] ... } ... ); WriteResult({ "nInserted" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo641.find();
This will produce the following output −
{ "_id" : ObjectId("5e9c31d46c954c74be91e6e2"), "ProductId" : 101, "ProductInformation" : [ { "ProductName" : "Product-1", "ProductPrice" : 1000 }, { "ProductName" : "Product-2", "ProductPrice" : 500 }, { "ProductName" : "Product-3", "ProductPrice" : 2000 }, { "ProductName" : "Product-4", "ProductPrice" : 3000 } ] }
Following is the query to get specific elements from embedded array in MongoDB
> db.demo641.aggregate([ ... {$unwind: "$ProductInformation"}, ... {$match: { "ProductInformation.ProductPrice": {$in :[1000, 2000]}} }, ... {$project: {_id: 0, ProductInformation: 1} } ... ]).pretty();
This will produce the following output −
{ "ProductInformation" : { "ProductName" : "Product-1", "ProductPrice" : 1000 } } { "ProductInformation" : { "ProductName" : "Product-3", "ProductPrice" : 2000 } }
- Related Articles
- Get count of array elements from a specific field in MongoDB documents?
- How to get values greater than a specific value from an embedded list in MongoDB?
- Return specific MongoDB embedded document
- How to get a specific object from array of objects inside specific MongoDB document?
- Fetch a specific document in MongoDB with array elements
- MongoDB query to get record beginning with specific element from an array?
- Get MongoDB documents that contains specific attributes in array
- Implement MongoDB $push in embedded document array?
- MongoDB Aggregate to get average from document and of array elements?
- MongoDB query to get only a specific number of elements
- How to get embedded data in a MongoDB document?
- How to project specific elements in an array field with MongoDB?
- MongoDB query to get only specific fields in nested array documents?
- Delete specific record from an array nested within another array in MongoDB?
- Deleting specific record from an array nested within another array in MongoDB

Advertisements