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
Set multiple conditions in MongoDB and fetch value in a range
Let us create a collection with documents −
> db.demo59.insertOne({"Values":50});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e286286cfb11e5c34d89923")
}
> db.demo59.insertOne({"Values":10});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e286288cfb11e5c34d89924")
}
> db.demo59.insertOne({"Values":58});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e28628ccfb11e5c34d89925")
}
> db.demo59.insertOne({"Values":78});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e28628fcfb11e5c34d89926")
}
Display all documents from a collection with the help of find() method −
> db.demo59.find();
This will produce the following output −
{ "_id" : ObjectId("5e286286cfb11e5c34d89923"), "Values" : 50 }
{ "_id" : ObjectId("5e286288cfb11e5c34d89924"), "Values" : 10 }
{ "_id" : ObjectId("5e28628ccfb11e5c34d89925"), "Values" : 58 }
{ "_id" : ObjectId("5e28628fcfb11e5c34d89926"), "Values" : 78 }
Here is the query to get value in a range −
> db.demo59.find({Values:{$gt:51, $lt:70}});
This will produce the following output −
{ "_id" : ObjectId("5e28628ccfb11e5c34d89925"), "Values" : 58 }Advertisements