Find MongoDB records with Price less than a specific value

MongoDBDatabaseBig Data Analytics

To check the records with Price less than a specific value, use $lt. Let us create a collection with documents −

> db.demo728.insertOne({Price:75});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5eab413c43417811278f589b")
}
> db.demo728.insertOne({Price:59});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5eab414043417811278f589c")
}
> db.demo728.insertOne({Price:79});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5eab414543417811278f589d")
}
> db.demo728.insertOne({Price:89});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5eab414843417811278f589e")
}

Display all documents from a collection with the help of find() method −

> db.demo728.find();

This will produce the following output −

{ "_id" : ObjectId("5eab413c43417811278f589b"), "Price" : 75 }
{ "_id" : ObjectId("5eab414043417811278f589c"), "Price" : 59 }
{ "_id" : ObjectId("5eab414543417811278f589d"), "Price" : 79 }
{ "_id" : ObjectId("5eab414843417811278f589e"), "Price" : 89 }

Following is the query to find records based on a condition −

> db.demo728.find({Price:{$lt:80}});

This will produce the following output −

{ "_id" : ObjectId("5eab413c43417811278f589b"), "Price" : 75 }
{ "_id" : ObjectId("5eab414043417811278f589c"), "Price" : 59 }
{ "_id" : ObjectId("5eab414543417811278f589d"), "Price" : 79 }
raja
Published on 15-May-2020 12:45:48
Advertisements