如何在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" }