
- 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
Retrieve only the queried element in an object array in MongoDB collection?
You can use projection operator $elemMatch to filter in queried element in an object array in MongoDB collection. To retrieve only the queried element in an object array in MongoDB, let us first create a collection with documents object array.
The query is as follows:
> db.objectArray.insert({"Persons":[ {"PersonName":"Adam","PersonSalary":25000},{"PersonName":"Larry","PersonSalary":27000 }]}); WriteResult({ "nInserted" : 1 }) > db.objectArray.insert({"Persons":[ {"PersonName":"David","PersonSalary":32000},{"PersonName":"Carol","PersonSalary":77000 }]}); WriteResult({ "nInserted" : 1 })
Now you can display all the documents with the help of find(). The query is as follows:
> db.objectArray.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c6bfadc68174aae23f5ef53"), "Persons" : [ { "PersonName" : "Adam", "PersonSalary" : 25000 }, { "PersonName" : "Larry", "PersonSalary" : 27000 } ] } { "_id" : ObjectId("5c6bfb0968174aae23f5ef54"), "Persons" : [ { "PersonName" : "David", "PersonSalary" : 32000 }, { "PersonName" : "Carol", "PersonSalary" : 77000 } ] }
Here is the query to retrieve only the queried element in an object array in MongoDB. Here In _id:0 is added to exclude the id property(do not show the _id property):
> db.objectArray.find({"Persons.PersonSalary":25000}, { _id:0,Persons:{$elemMatch:{"PersonSalary":25000}}}).pretty();
The following is the output:
{ "Persons" : [ { "PersonName" : "Adam", "PersonSalary" : 25000 } ] }
Alternate way to do the above task is with the help of projection operator $. The query is as follows:
> db.objectArray.find({"Persons.PersonSalary":25000}, { _id:0,'Persons.$':1}).pretty();
The following is the output:
{ "Persons" : [ { "PersonName" : "Adam", "PersonSalary" : 25000 } ] }
- Related Articles
- Display only an element found in an array in MongoDB?
- Removing an array element from a MongoDB collection
- Retrieve the position in an Array in MongoDB?
- Increment value of an array element with array object in MongoDB
- Retrieve data from a MongoDB collection?
- Removing an object in a child collection in MongoDB?
- Removing an array element from MongoDB collection using update() and $pull
- How to retrieve documents from a collection in MongoDB?
- Query to retrieve multiple items in an array in MongoDB?
- Get the maximum element in MongoDB collection?
- Increment a property value of an element in array object with MongoDB
- MongoDB query for capped sub-collection in an array
- MongoDB query to pull array element from a collection?
- Retrieve key and values from object in an array JavaScript
- How to retrieve a nested object in MongoDB?

Advertisements