要查找高于特定值的值,以下是在MongoDB中使用$gte的语法-
db.yourCollectionName.find({yourFieldName:{$gte:yourValue}});
让我们创建一个包含文档的集合-
> 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") }
在find()方法的帮助下显示集合中的所有文档-
> db.demo571.find();
这将产生以下输出-
{ "_id" : ObjectId("5e909b3439cfeaaf0b97b587"), "Price" : 140 } { "_id" : ObjectId("5e909b3639cfeaaf0b97b588"), "Price" : 100 } { "_id" : ObjectId("5e909b3839cfeaaf0b97b589"), "Price" : 110 } { "_id" : ObjectId("5e909b3c39cfeaaf0b97b58a"), "Price" : 240 }
以下是从文档中获取高于特定值的值的查询-
> db.demo571.find({Price:{$gte:140}});
这将产生以下输出-
{ "_id" : ObjectId("5e909b3439cfeaaf0b97b587"), "Price" : 140 } { "_id" : ObjectId("5e909b3c39cfeaaf0b97b58a"), "Price" : 240 }