
- 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
Match element in array of MongoDB?
You can use $or operator along with limit(1) to match element in array. Let us first create a collection with documents −
> db.matchElementInArrayDemo.insertOne( ... { ... "StudentName" : "Chris" , ... "StudentOtherDetails" : ... [ ... {"StudentCountryName" : "US" , "StudentSkills" : "MongoDB"}, ... {"StudentCountryName" : "UK" , "StudentSkills" : "Java"} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd423282cba06f46efe9ee2") } > db.matchElementInArrayDemo.insertOne( ... { ... "StudentName" : "Chris" , ... "StudentOtherDetails" : ... [ ... {"StudentCountryName" : "AUS" , "StudentSkills" : "PHP"}, ... {"StudentCountryName" : "US" , "StudentSkills" : "MongoDB"} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd423412cba06f46efe9ee3") }
Following is the query to display all documents from a collection with the help of find() method −
> db.matchElementInArrayDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd423282cba06f46efe9ee2"), "StudentName" : "Chris", "StudentOtherDetails" : [ { "StudentCountryName" : "US", "StudentSkills" : "MongoDB" }, { "StudentCountryName" : "UK", "StudentSkills" : "Java" } ] } { "_id" : ObjectId("5cd423412cba06f46efe9ee3"), "StudentName" : "Chris", "StudentOtherDetails" : [ { "StudentCountryName" : "AUS", "StudentSkills" : "PHP" }, { "StudentCountryName" : "US", "StudentSkills" : "MongoDB" } ] }
Here is the query to match element in array of MongoDB −
> db.matchElementInArrayDemo.find( { $or : [ {"StudentOtherDetails.StudentCountryName": "US" } ,{"StudentOtherDetails.StudentSkills": "MongoDB" } ] } ).limit(1);
This will produce the following output −
{ "_id" : ObjectId("5cd423282cba06f46efe9ee2"), "StudentName" : "Chris", "StudentOtherDetails" : [ { "StudentCountryName" : "US", "StudentSkills" : "MongoDB" }, { "StudentCountryName" : "UK", "StudentSkills" : "Java" } ] }
- Related Articles
- MongoDB query to match and remove element from an array?
- Implement array match in MongoDB?
- MongoDB query to match each element in a documents array to a condition?
- Update Array element in MongoDB?
- Accessing inner element of JSON array in MongoDB?
- Match multiple criteria inside an array with MongoDB?
- Match MongoDB documents with fields not containing values in array?
- Increment value of an array element with array object in MongoDB
- Match ID and fetch documents with $eq in MongoDB in case of array?
- Find result within array of objects and match email address field in MongoDB?
- How to remove array element in MongoDB?
- Get index of given element in array field in MongoDB?
- Pull an element in sub of sub-array in MongoDB?
- Update field in exact element array in MongoDB?
- Remove null element from MongoDB array?

Advertisements