Find value above a specific value in MongoDB documents?

MongoDBDatabaseBig Data Analytics

To find values above a specific value, the following is the syntax using $gte in MongoDB −

db.yourCollectionName.find({yourFieldName:{$gte:yourValue}});

Let us create a collection with documents −

> db.demo571.insertOne({"Price":140});{
   "acknowledged" : true, "insertedId" : ObjectId("5e909b3439cfeaaf0b97b587")
}
> db.demo571.insertOne({"Price":100});{
   "acknowledged" : true, "insertedId" : ObjectId("5e909b3639cfeaaf0b97b588")
}
> db.demo571.insertOne({"Price":110});{
   "acknowledged" : true, "insertedId" : ObjectId("5e909b3839cfeaaf0b97b589")
}
> db.demo571.insertOne({"Price":240});{
   "acknowledged" : true, "insertedId" : ObjectId("5e909b3c39cfeaaf0b97b58a")
}

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

> db.demo571.find();

This will produce the following output −

{ "_id" : ObjectId("5e909b3439cfeaaf0b97b587"), "Price" : 140 }
{ "_id" : ObjectId("5e909b3639cfeaaf0b97b588"), "Price" : 100 }
{ "_id" : ObjectId("5e909b3839cfeaaf0b97b589"), "Price" : 110 }
{ "_id" : ObjectId("5e909b3c39cfeaaf0b97b58a"), "Price" : 240 }

Following is the query to get values from documents above a specific value −

> db.demo571.find({Price:{$gte:140}});

This will produce the following output −

{ "_id" : ObjectId("5e909b3439cfeaaf0b97b587"), "Price" : 140 }
{ "_id" : ObjectId("5e909b3c39cfeaaf0b97b58a"), "Price" : 240 }
raja
Published on 15-May-2020 09:08:14
Advertisements