
- 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
Is it possible to use MongoDB to query for entries that have a particular value in a field in an object in an array?
Yes, to query for a field in an object in the array with MongoDB, use the following syntax −
db.yourCollectionName.find({"yourOuterFieldName": { $elemMatch: { "yourInnerFieldName": "yourValue" } } } ).pretty();
To understand the above concept, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.objectInAnArrayDemo.insertOne({ "StudentDetails": [{ "StudentName": "John", "StudentMessage": "Hi"}, {"StudentName": "Larry", "StudentMessage": "Hello"}]}) { "acknowledged" : true, "insertedId" : ObjectId("5c92635d36de59bd9de06381") } > db.objectInAnArrayDemo.insertOne({ "StudentDetails": [{ "StudentName": "Carol", "StudentMessage": "Hello"}, {"StudentName": "David", "StudentMessage": "Good Morning"}]}) { "acknowledged" : true, "insertedId" : ObjectId("5c92637936de59bd9de06382") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.objectInAnArrayDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c92635d36de59bd9de06381"), "StudentDetails" : [ { "StudentName" : "John", "StudentMessage" : "Hi" }, { "StudentName" : "Larry", "StudentMessage" : "Hello" } ] } { "_id" : ObjectId("5c92637936de59bd9de06382"), "StudentDetails" : [ { "StudentName" : "Carol", "StudentMessage" : "Hello" }, { "StudentName" : "David", "StudentMessage" : "Good Morning" } ] }
Here is the query for a field in an object in the array with MongoDB −
> db.objectInAnArrayDemo.find({"StudentDetails": { $elemMatch: { "StudentMessage": "Good Morning" } } } ).pretty();
The following is the output −
{ "_id" : ObjectId("5c92637936de59bd9de06382"), "StudentDetails" : [ { "StudentName" : "Carol", "StudentMessage" : "Hello" }, { "StudentName" : "David", "StudentMessage" : "Good Morning" } ] }
- Related Articles
- Is it possible to use MongoDB field value as a pattern in $regex?
- MongoDB query to fetch a document that does not have a particular field?
- Is it possible to use MongoDB field value as pattern in $regex?
- MongoDB query to access an object in an array
- MongoDB query for Partial Object in an array
- Query an array in MongoDB to fetch a specific value
- MongoDB query to match documents that contain an array field
- MongoDB query to replace value in an array?
- Is it possible to cast in a MongoDB Query?
- MongoDB query to change simple field into an object?
- MongoDB query to remove empty objects in an object-array?
- Add a field to an embedded document in an array in MongoDB?
- How to insert an item to an array that is inside an object in MongoDB?
- Query a nested field within an array with MongoDB
- Is it possible to make a case-insensitive query in MongoDB?

Advertisements