要检查价格小于特定值的记录,请使用$lt。让我们创建一个包含文档的集合-
> 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") }
在find()方法的帮助下显示集合中的所有文档-
> db.demo728.find();
这将产生以下输出-
{ "_id" : ObjectId("5eab413c43417811278f589b"), "Price" : 75 } { "_id" : ObjectId("5eab414043417811278f589c"), "Price" : 59 } { "_id" : ObjectId("5eab414543417811278f589d"), "Price" : 79 } { "_id" : ObjectId("5eab414843417811278f589e"), "Price" : 89 }
以下是根据条件查找记录的查询-
> db.demo728.find({Price:{$lt:80}});
这将产生以下输出-
{ "_id" : ObjectId("5eab413c43417811278f589b"), "Price" : 75 } { "_id" : ObjectId("5eab414043417811278f589c"), "Price" : 59 } { "_id" : ObjectId("5eab414543417811278f589d"), "Price" : 79 }