Set multiple conditions in MongoDB and fetch value in a range

MongoDBDatabaseBig Data Analytics

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 }
raja
Published on 03-Apr-2020 16:11:53
Advertisements