
- 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 the matching document inside an array in MongoDB?
To get the matching document, use $elemMatch. Let us first create a collection with documents −
> db.getMatchingDocumentDemo.insertOne( { _id :1, "UserDetails":[ { "UserName":"John", "UserAge":23 } ] } ); { "acknowledged" : true, "insertedId" : 1 } > db.getMatchingDocumentDemo.insertOne( { _id :2, "UserDetails":[ { "UserName":"Larry", "UserAge":24 } ] } ); { "acknowledged" : true, "insertedId" : 2 }
Following is the query to display all documents from a collection with the help of find() method −
> db.getMatchingDocumentDemo.find().pretty();
This will produce the following output −
{ "_id" : 1, "UserDetails" : [ { "UserName" : "John", "UserAge" : 23 } ] } { "_id" : 2, "UserDetails" : [ { "UserName" : "Larry", "UserAge" : 24 } ] }
Following is the query to get the matching document inside an array in MongoDB −
> db.getMatchingDocumentDemo.find({UserDetails: {$elemMatch: {UserAge: 24}}});
This will produce the following output −
{ "_id" : 2, "UserDetails" : [ { "UserName" : "Larry", "UserAge" : 24 } ] }
- Related Articles
- Get array items inside a MongoDB document?
- How to project specific fields from a document inside an array in Mongodb?
- How to get a specific object from array of objects inside specific MongoDB document?
- MongoDB syntax for updating an object inside an array within a document?
- MongoDB query to push document into an array
- Native Querying MongoDB inside array and get the count
- Update elements inside an array in MongoDB?
- How to match multiple criteria inside an array with MongoDB?
- MongoDB exact array matching
- How to add a sub-document to sub-document array in MongoDB?
- How to query a document in MongoDB comparing fields from an array?
- How to pull an array element (which is a document) in MongoDB?
- How to push new items to an array inside of an object in MongoDB?
- How to insert an item to an array that is inside an object in MongoDB?
- How to get embedded data in a MongoDB document?

Advertisements