
- 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 find value in array with multiple criteria (range)
To find value in an array within a range, use $gt and $lt. Let us create a collection with documents −
> db.demo341.insertOne({ ... "Name": "Chris", ... "productDetails" : [ ... { ... "ProductPrice" : { ... "Price" : 800 ... } ... }, ... { ... "ProductPrice" : { ... "Price" : 400 ... } ... }, ... { ... "ProductPrice" : { ... "Price" : 300 ... } ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e53ed5cf8647eb59e5620a7") } > db.demo341.insertOne({ ... "Name": "Chris", ... "productDetails" : [ ... { ... "ProductPrice" : { ... "Price" : 1000 ... } ... }, ... { ... "ProductPrice" : { ... "Price" : 1200 ... } ... }, ... { ... "ProductPrice" : { ... "Price" : 1300 ... } ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e53edc1f8647eb59e5620a8") }
Display all documents from a collection with the help of find() method −
> db.demo341.find();
This will produce the following output −
{ "_id" : ObjectId("5e53ed5cf8647eb59e5620a7"), "Name" : "Chris", "productDetails" : [ { "ProductPrice" : { "Price" : 800 } }, { "ProductPrice" : { "Price" : 400 } }, { "ProductPrice" : { "Price" : 300 } } ] } { "_id" : ObjectId("5e53edc1f8647eb59e5620a8"), "Name" : "Chris", "productDetails" : [ { "ProductPrice" : { "Price" : 1000 } }, { "ProductPrice" : { "Price" : 1200 } }, { "ProductPrice" : { "Price" : 1300 } } ] }
Following is the query to find value in the array with multiple criteria −
> db.demo341.aggregate([ ... { "$match": { ... "productDetails": { ... "$elemMatch": { ... "ProductPrice.Price": { ... "$gt": 600, ... "$lt": 900 ... } ... } ... } ... }} ... ]).pretty();
This will produce the following output −
{ "_id" : ObjectId("5e53ed5cf8647eb59e5620a7"), "Name" : "Chris", "productDetails" : [ { "ProductPrice" : { "Price" : 800 } }, { "ProductPrice" : { "Price" : 400 } }, { "ProductPrice" : { "Price" : 300 } } ] }
- Related Articles
- Find value in a MongoDB Array with multiple criteria?
- MongoDB query to find documents having two values in an array conforming to multiple criteria?
- Match multiple criteria inside an array with MongoDB?
- How to match multiple criteria inside an array with MongoDB?
- MongoDB query to find and return subdocument with criteria?
- Query MongoDB with length criteria?
- How to find specific array elements in MongoDB document with query and filter with range?
- Which MongoDB query finds same value multiple times in an array?
- MongoDB query to find multiple matchings inside array of objects?
- MongoDB query to pull multiple values from array
- What kind of MongoDB query finds same value multiple times in an array?
- Query to retrieve multiple items in an array in MongoDB?
- MongoDB query to replace value in an array?
- MongoDB query to $pull / $unset with multiple conditions?
- Set multiple conditions in MongoDB and fetch value in a range

Advertisements