在文档的第三级查询Mongo

时间:2017-03-19 17:58:26

标签: arrays mongodb

在Mongo DB中,我有这样的结构:

for (int i = 0; i<line.length();i++){
    line.charAt(i)++;
}

我想要一个过滤器来获取PoolerId = 583a2547499050b6c056c8a0的Poolers,我没有找到Mongo的正确语法。我尝试了各种各样的事情:

{
  "_id" : ObjectId("58cc95a68a7d830a708243fc"), //Hockey Pool
  "Name" : "Pool des gars",
  "Poolers" : null, // Pooler for that pool, null when pooler are in subgroups
  "PoolerGroups" : [{ // Pool group who contains Pooler
      "Name" : "Salon",
      "Code" : "SA",
      "Poolers" : [{ 
          "UserRight" : 1,
          "PoolerId" : ObjectId("583a2547499050b6c056c8a0"), // User id for 1 pooler
          "PlayersIds" : null // Try to find that pooler to update the PlayerIds list of that pooler
        }, {
          "UserRight" : 5,
          "PoolerId" : ObjectId("583a2547499050b6c056c8a2"),
          "PlayersIds" : null
        }, {
          "UserRight" : 1,
          "PoolerId" : ObjectId("583a2547499050b6c056c8a7"),
          "PlayersIds" : null
        }, {
          "UserRight" : 5,
          "PoolerId" : ObjectId("583a2547499050b6c056c8ac"),
          "PlayersIds" : null
        }, {
          "UserRight" : 1,
          "PoolerId" : ObjectId("583a2547499050b6c056c8ad"),
          "PlayersIds" : null
        }]
    }, {
      "Name" : "Cuisine",
      "Code" : "CU",
      "Poolers" : [{
          "UserRight" : 37,
          "PoolerId" : ObjectId("583a2547499050b6c056c8a1"),
          "PlayersIds" : null
        }, {
          "UserRight" : 1,
          "PoolerId" : ObjectId("583a2547499050b6c056c8a5"),
          "PlayersIds" : null
        }, {
          "UserRight" : 1,
          "PoolerId" : ObjectId("583a2547499050b6c056c8a9"),
          "PlayersIds" : null
        }, {
          "UserRight" : 1,
          "PoolerId" : ObjectId("583a2547499050b6c056c8aa"),
          "PlayersIds" : null
        }, {
          "UserRight" : 1,
          "PoolerId" : ObjectId("583a2547499050b6c056c8ab"),
          "PlayersIds" : null
        }]
    }, {
      "Name" : "Sous-sol",
      "Code" : "SS",
      "Poolers" : [{
          "UserRight" : 1,
          "PoolerId" : ObjectId("583a2547499050b6c056c8a3"),
          "PlayersIds" : null
        }, {
          "UserRight" : 1,
          "PoolerId" : ObjectId("583a2547499050b6c056c8a4"),
          "PlayersIds" : null
        }, {
          "UserRight" : 1,
          "PoolerId" : ObjectId("583a2547499050b6c056c8a6"),
          "PlayersIds" : null
        }, {
          "UserRight" : 1,
          "PoolerId" : ObjectId("583a2547499050b6c056c8a8"),
          "PlayersIds" : null
        }, {
          "UserRight" : 1,
          "PoolerId" : ObjectId("583a2547499050b6c056c8ae"),
          "PlayersIds" : null
        }]
    }]
}

但是每个人都没有回报,有人知道出了什么问题吗?我需要这个来获得一个过滤器来更新PlayerIds列表。我在Mongo上使用c#驱动程序。

2 个答案:

答案 0 :(得分:0)

Frip。 你尝试这样的查询..

db.PoolerGroups.find({},{_id: 0, Poolers: {$elemMatch: {"PoolerId" : ObjectId("583a2547499050b6c056c8a0")}}});

答案 1 :(得分:0)

你错过了 ObjectId 包装器。在数据库中,那些PoolerId值是BSON Objectid data type,您无法将它们与普通字符串进行比较。

而不是此查询:

{ "PoolerGroups.Poolers.PoolerId" : "583a2547499050b6c056c8a0" } 

你需要它像这样:

{ "PoolerGroups.Poolers.PoolerId" : ObjectId("583a2547499050b6c056c8a0") }