
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MongoDB Query: Where All Array Items Are Less Than a Specified Condition
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 less than a specified condition −
> db.arrayElementsNotGreaterThanDemo.find({Scores: {$not: {$gt:45}}});
This will produce the following output −
{ "_id" : ObjectId("5cd9ea13b50a6c6dd317adb4"), "Scores" : [ 32, 33, 34, 40 ] }
Advertisements