
- 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 where all array items are greater than a specified condition?
You can use $gt operator for this. Let us first create a collection with documents −
> db.arrayElementsNotGreaterThanDemo.insertOne({"Scores":[89,43,32,45]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9e9f9b50a6c6dd317adb3") } > db.arrayElementsNotGreaterThanDemo.insertOne({"Scores":[32,33,34,40]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9ea13b50a6c6dd317adb4") } > db.arrayElementsNotGreaterThanDemo.insertOne({"Scores":[45,56,66,69]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9ea25b50a6c6dd317adb5") } > db.arrayElementsNotGreaterThanDemo.insertOne({"Scores":[46,66,77,88]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9ea3cb50a6c6dd317adb6") }
Following is the query to display all documents from a collection with the help of find() method −
> db.arrayElementsNotGreaterThanDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd9e9f9b50a6c6dd317adb3"), "Scores" : [ 89, 43, 32, 45 ] } { "_id" : ObjectId("5cd9ea13b50a6c6dd317adb4"), "Scores" : [ 32, 33, 34, 40 ] } { "_id" : ObjectId("5cd9ea25b50a6c6dd317adb5"), "Scores" : [ 45, 56, 66, 69 ] } { "_id" : ObjectId("5cd9ea3cb50a6c6dd317adb6"), "Scores" : [ 46, 66, 77, 88 ] }
Following is the query where all array items are greater than a specified condition −
> db.arrayElementsNotGreaterThanDemo.find({Scores: {$gt:45}});
This will produce the following output −
{ "_id" : ObjectId("5cd9e9f9b50a6c6dd317adb3"), "Scores" : [ 89, 43, 32, 45 ] } { "_id" : ObjectId("5cd9ea25b50a6c6dd317adb5"), "Scores" : [ 45, 56, 66, 69 ] } { "_id" : ObjectId("5cd9ea3cb50a6c6dd317adb6"), "Scores" : [ 46, 66, 77, 88 ] }
- Related Articles
- MongoDB query where all array items are less than a specified condition?
- Query for documents where array size is greater than 1 in MongoDB?
- MongoDB query to filter object where all elements from nested array match the condition
- Query nested array by more than one condition in MongoDB
- MongoDB query to match documents with array values greater than a specific value
- How to query all items in MongoDB?
- MySQL GROUP BY with WHERE clause and condition count greater than 1?
- MongoDB query to insert an array element with a condition?
- Find the Number of segments where all elements are greater than X using C++
- MongoDB query with $all in array
- MongoDB query to update an array element matching a condition using $push?
- MongoDB query to match each element in a documents array to a condition?
- How to select where sum of fields is greater than a value in MongoDB?
- Query to retrieve multiple items in an array in MongoDB?
- Program to filter all values which are greater than x in an array

Advertisements