- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find value in a MongoDB Array with multiple criteria?
To find value in the array with multiple criteria, for example, you can use $elemMatch along with $gt and $lt. The syntax is as follows −
db.yourCollectionName.find({yourFieldName:{$elemMatch:{$gt:yourNegativeValue,$lt:yourPo sitiveValue}}}).pretty();
To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.findValueInArrayWithMultipleCriteriaDemo.insertOne({"StudentName":"Larry","StudentMarks":[-150,150]}); { "acknowledged" : true, "insertedId" : ObjectId("5c77daf6fc4e719b197a12f5") } > db.findValueInArrayWithMultipleCriteriaDemo.insertOne({"StudentName":"Mike","StudentMarks":[19]}); { "acknowledged" : true, "insertedId" : ObjectId("5c77db09fc4e719b197a12f6") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.findValueInArrayWithMultipleCriteriaDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c77daf6fc4e719b197a12f5"), "StudentName" : "Larry", "StudentMarks" : [ -150, 150 ] } { "_id" : ObjectId("5c77db09fc4e719b197a12f6"), "StudentName" : "Mike", "StudentMarks" : [ 19 ] }
Here is the query to find value in the array with multiple criteria. For example, here we are considering marks greater than -20 and less than 20 −
> db.findValueInArrayWithMultipleCriteriaDemo.find({StudentMarks:{$elemMatch:{$gt:-20,$lt:20}}}).pretty();
The following is the output −
{ "_id" : ObjectId("5c77db09fc4e719b197a12f6"), "StudentName" : "Mike", "StudentMarks" : [ 19 ] }
- Related Articles
- MongoDB query to find value in array with multiple criteria (range)
- Match multiple criteria inside an array with MongoDB?
- How to match multiple criteria inside an array with MongoDB?
- MongoDB query to find documents having two values in an array conforming to multiple criteria?
- Query MongoDB with length criteria?
- MongoDB query to find and return subdocument with criteria?
- MongoDB find by multiple array items?
- Find document with array that contains a specific value in MongoDB
- MongoDB find by multiple array items using $in?
- Which MongoDB query finds same value multiple times in an array?
- How to update array with multiple conditions in MongoDB
- Update the last row with search criteria in MongoDB?
- How to find only a single document satisfying the criteria in MongoDB?
- Find MongoDB document with array containing the maximum occurrence of a specific value
- Aggregate multiple arrays into one huge array with MongoDB?

Advertisements