如何针对 MongoDB 文档中的哈希执行 $gt?
$gt 用于大于,其中选择值为指定值以上的字段的文档。我们首先用文档创建集合 −
> db.performQueryDemo.insertOne({"PlayerDetails":{"PlayerScore":1000,"PlayerLevel":2},"PlayerName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7eba41a844af18acdffa9") } > db.performQueryDemo.insertOne({"PlayerDetails":{"PlayerScore":0,"PlayerLevel":1},"PlayerName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ebbb1a844af18acdffaa") } > db.performQueryDemo.insertOne({"PlayerDetails":{"PlayerScore":-10,"PlayerLevel":0},"PlayerName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ebd41a844af18acdffab") } > db.performQueryDemo.insertOne({"PlayerDetails":{"PlayerScore":1,"PlayerLevel":1},"PlayerName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ebe31a844af18acdffac") }
下面是使用 find() 方法显示集合中所有文档的查询 −
> db.performQueryDemo.find().pretty();
这将产生以下输出 −
{ "_id" : ObjectId("5cd7eba41a844af18acdffa9"), "PlayerDetails" : { "PlayerScore" : 1000, "PlayerLevel" : 2 }, "PlayerName" : "Chris" } { "_id" : ObjectId("5cd7ebbb1a844af18acdffaa"), "PlayerDetails" : { "PlayerScore" : 0, "PlayerLevel" : 1 }, "PlayerName" : "Robert" } { "_id" : ObjectId("5cd7ebd41a844af18acdffab"), "PlayerDetails" : { "PlayerScore" : -10, "PlayerLevel" : 0 }, "PlayerName" : "Larry" } { "_id" : ObjectId("5cd7ebe31a844af18acdffac"), "PlayerDetails" : { "PlayerScore" : 1, "PlayerLevel" : 1 }, "PlayerName" : "David" }
下面是针对 MongoDB 文档中的哈希执行 $gt 的查询。在这里,我们将获得得分高于 0 的球员记录 −
> db.performQueryDemo.find({"PlayerDetails.PlayerScore":{$gt:0}});
这将产生以下输出 −
{ "_id" : ObjectId("5cd7eba41a844af18acdffa9"), "PlayerDetails" : { "PlayerScore" : 1000, "PlayerLevel" : 2 }, "PlayerName" : "Chris" } { "_id" : ObjectId("5cd7ebe31a844af18acdffac"), "PlayerDetails" : { "PlayerScore" : 1, "PlayerLevel" : 1 }, "PlayerName" : "David" }
广告