
- 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
Filter by several array elements in MongoDB?
For this, you can use $elemMatch operator. The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria. Let us first create a collection with documents −
> db.filterBySeveralElementsDemo.insertOne( "_id":100, "StudentDetails": [ { "StudentName": "John", "StudentCountryName": "US", }, { "StudentName": "Carol", "StudentCountryName": "UK" } ] } ); { "acknowledged" : true, "insertedId" : 100 } > db.filterBySeveralElementsDemo.insertOne( { "_id":101, "StudentDetails": [ { "StudentName": "Sam", "StudentCountryName": "AUS", }, { "StudentName": "Chris", "StudentCountryName": "US" } ] } ); { "acknowledged" : true, "insertedId" : 101 }
Following is the query to display all documents from a collection with the help of find() method −
> db.filterBySeveralElementsDemo.find().pretty();
This will produce the following output −
{ "_id" : 100, "StudentDetails" : [ { "StudentName" : "John", "StudentCountryName" : "US" }, { "StudentName" : "Carol", "StudentCountryName" : "UK" } ] } { "_id" : 101, "StudentDetails" : [ { "StudentName" : "Sam", "StudentCountryName" : "AUS" }, { "StudentName" : "Chris", "StudentCountryName" : "US" } ] }
Following is the query to filter by several array elements −
> db.filterBySeveralElementsDemo.find({ StudentDetails: { $elemMatch: { StudentName: 'Sam', StudentCountryName: 'AUS' }}}).pretty();
This will produce the following output −
{ "_id" : 101, "StudentDetails" : [ { "StudentName" : "Sam", "StudentCountryName" : "AUS" }, { "StudentName" : "Chris", "StudentCountryName" : "US" } ] }
- Related Articles
- MongoDB query to filter by several array elements?
- How to filter array elements in MongoDB?
- How to find specific array elements in MongoDB document with query and filter with range?
- How to filter array in subdocument with MongoDB?
- MongoDB query to filter object where all elements from nested array match the condition
- Filter sub documents by sub document in MongoDB?
- How to filter documents based on an array in MongoDB?
- Filter query on array of embedded document with MongoDB?
- Remove elements from array using JavaScript filter - JavaScript
- JavaScript filter array by multiple strings?
- Querying array elements with MongoDB?
- Update elements inside an array in MongoDB?
- Update multiple elements in an array in MongoDB?
- How to filter an array from all elements of another array – JavaScript?
- Get specific elements from embedded array in MongoDB?

Advertisements