
- 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 documents with array values greater than a specific value
You can use $elemMatch. The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.
Let us create a collection with documents −
> db.demo701.insertOne({"ListOfValues":[100,200,300]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6e8cf551299a9f98c93b0") } > db.demo701.insertOne({"ListOfValues":[500,700,1000]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6e8d8551299a9f98c93b1") } > db.demo701.insertOne({"ListOfValues":[300,350,450]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6e8e1551299a9f98c93b2") }
Display all documents from a collection with the help of find() method −
> db.demo701.find();
This will produce the following output −
{ "_id" : ObjectId("5ea6e8cf551299a9f98c93b0"), "ListOfValues" : [ 100, 200, 300 ] } { "_id" : ObjectId("5ea6e8d8551299a9f98c93b1"), "ListOfValues" : [ 500, 700, 1000 ] } { "_id" : ObjectId("5ea6e8e1551299a9f98c93b2"), "ListOfValues" : [ 300, 350, 450 ] }
Following is the query to match documents with array values greater than a specific value −
> db.demo701.find({"ListOfValues":{$elemMatch:{$gt:500}}});
This will produce the following output −
{ "_id" : ObjectId("5ea6e8d8551299a9f98c93b1"), "ListOfValues" : [ 500, 700, 1000 ] }
- Related Articles
- Match MongoDB documents with field value greater than a specific number and fetch them?
- Query for documents where array size is greater than 1 in MongoDB?
- Match MongoDB documents with fields not containing values in array?
- MongoDB query (aggregation framework) to match a specific field value
- MongoDB query to match documents that contain an array field
- MongoDB query for documents whose array elements does not have a specific value
- MongoDB query to find matching documents given an array with values?
- How to get values greater than a specific value from an embedded list in MongoDB?
- MongoDB query to match each element in a documents array to a condition?
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- MongoDB query to add up the values of a specific field in documents
- MongoDB query to get only specific fields in nested array documents?
- MongoDB query to find documents with specific FirstName and LastName
- MongoDB aggregation to fetch documents with specific field value?
- MongoDB query to display documents with a specific name irrespective of case

Advertisements