-1

In Mongo DB, I have a structure like this :

{
  "_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
        }]
    }]
}

I want a filter to get thoe Poolers with PoolerId = 583a2547499050b6c056c8a0, I don't find the correct syntax with Mongo. I tried various thing like :

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

But everythings return nothing, does somebody know what is wrong? I need this to get a filter to update the PlayerIds list. I use c# driver with Mongo.

Frip
  • 129
  • 1
  • 8
  • Possible duplicate of [Retrieve only the queried element in an object array in MongoDB collection](http://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection) – s7vr Mar 19 '17 at 18:13

2 Answers2

0

Frip. you try like this query..

db.PoolerGroups.find({},{_id: 0, Poolers: {$elemMatch: {"PoolerId" : ObjectId("583a2547499050b6c056c8a0")}}});
sathish anish
  • 463
  • 1
  • 5
  • 17
0

You're missing out the ObjectId wrapper. In the database, those PoolerId values are a BSON Objectid data type, and you can't compare them to a plain string.

Instead of this query:

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

you need it to be like this:

{ "PoolerGroups.Poolers.PoolerId" : ObjectId("583a2547499050b6c056c8a0") } 
Vince Bowdren
  • 8,326
  • 3
  • 31
  • 56
  • Thanks for the ObjectId, my filter now seem OK. A friend told me it is impossible to update entries at third level... Is that true or somebody else have a solution – Frip Mar 25 '17 at 14:57
  • No, it _is_ possible to update entries nested at the third level, or or at any nested level. You just have to get the update command right; do some reading on the [$ positional operator](https://docs.mongodb.com/manual/reference/operator/update/positional/), you will need it. – Vince Bowdren Mar 25 '17 at 15:11