
- 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
MongoDB query to match each element in a documents array to a condition?
You can use every() in MongoDB for this. Let us create a collection with documents −
> db.arrayConditionDemo.insertOne({"Name":"John","Marks":[40,43,45]}); { "acknowledged" : true, "insertedId" : ObjectId("5cbdbd06de8cc557214c0e1a") } > db.arrayConditionDemo.insertOne({"Name":"Mike","Marks":[45]}); { "acknowledged" : true, "insertedId" : ObjectId("5cbdbd17de8cc557214c0e1b") } > db.arrayConditionDemo.insertOne({"Name":"Chris","Marks":[43,45,59,69,78,89]}); { "acknowledged" : true, "insertedId" : ObjectId("5cbdbd3cde8cc557214c0e1c") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.arrayConditionDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cbdbd06de8cc557214c0e1a"), "Name" : "John", "Marks" : [ 40, 43, 45 ] } { "_id" : ObjectId("5cbdbd17de8cc557214c0e1b"), "Name" : "Mike", "Marks" : [ 45 ] } { "_id" : ObjectId("5cbdbd3cde8cc557214c0e1c"), "Name" : "Chris", "Marks" : [ 43, 45, 59, 69, 78, 89 ] }
Following is the query to match each element in a documents array to a condition −
> db.arrayConditionDemo.find("return this.Marks.every(function(m) { return (m >= 40 && m<= 100) })");
This will produce the following output −
{ "_id" : ObjectId("5cbdbd06de8cc557214c0e1a"), "Name" : "John", "Marks" : [ 40, 43, 45 ] } { "_id" : ObjectId("5cbdbd17de8cc557214c0e1b"), "Name" : "Mike", "Marks" : [ 45 ] } { "_id" : ObjectId("5cbdbd3cde8cc557214c0e1c"), "Name" : "Chris", "Marks" : [ 43, 45, 59, 69, 78, 89 ] }
- Related Articles
- MongoDB query to insert an array element with a condition?
- MongoDB query to match documents that contain an array field
- MongoDB query to match documents with array values greater than a specific value
- MongoDB query to update an array element matching a condition using $push?
- How to query documents by a condition on the subdocument in MongoDB?
- MongoDB query to collectively match intersection of documents against a field
- MongoDB query to match documents whose _id is in an array as part of a subdocument?
- MongoDB query to match and remove element from an array?
- Get fields from multiple sub-documents that match a condition in MongoDB?
- MongoDB query to filter object where all elements from nested array match the condition
- Fetch multiple documents in MongoDB query with OR condition?
- MongoDB query to update each field of documents in collection with a formula?
- MongoDB query to pull array element from a collection?
- Match element in array of MongoDB?
- MongoDB query to skip documents

Advertisements