如何使用$ lte,$ in和$ not查询MongoDB的值以获取特定值?
首先让我们创建一个包含文档的集合-
> db.demo130.insertOne( ... { ... ... "PlayerDetails":[{Score:56},{Score:78},{Score:89},{Score:97}] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e3065bf68e7f832db1a7f6d") } > db.demo130.insertOne( ... { ... ... "PlayerDetails":[{Score:45},{Score:56}] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e3065c068e7f832db1a7f6e") }
在find()
方法的帮助下显示集合中的所有文档-
> db.demo130.find();
这将产生以下输出-
{ "_id" : ObjectId("5e3065bf68e7f832db1a7f6d"), "PlayerDetails" : [ { "Score" : 56 }, { "Score" : 78 }, { "Score" : 89 }, { "Score" : 97 } ] } { "_id" : ObjectId("5e3065c068e7f832db1a7f6e"), "PlayerDetails" : [ { "Score" : 45 }, { "Score" : 56 } ] }
以下是要获取的查询-
> db.demo130.find({ ... "PlayerDetails.Score": { ... "$eq": 56, ... "$not": { "$gt": 56} ... } ... })
这将产生以下输出-
{ "_id" : ObjectId("5e3065c068e7f832db1a7f6e"), "PlayerDetails" : [ { "Score" : 45 }, { "Score" : 56 } ] }