简体   繁体   中英

Triple Nested Array MongoDB Query

I'd like to find the correct query to return all Boards within the Boards collection where the member id matches 1. Any takers?

My schema as follows:

Within the 'Boards' collection,

      { name: 'Board One',
    teams: [
      { name: 'Team One',
        members: [
            { id: '1', name: 'Garrett' },
            { id: '2', name: 'Sarah' }
        ]
      },
      { name: 'Team Two',
        members: [
            { id: '1', name: 'Garrett' },
            { id: '2', name: 'Jeff' }
        ]
      }
    ]
  },`{ name: 'Board Two',
    teams: [
      { name: 'Team One',
        members: [
            { id: '1', name: 'Garrett' },
            { id: '2', name: 'Sarah' }
        ]
      },
      { name: 'Team Three',
        members: [
            { id: '1', name: 'Jim' },
            { id: '2', name: 'Samson' }
        ]
      }
    ]

I'm trying to query a object within an array, within array of objects, within a collection. I've tried many variations on the following query..

Boards.find({
  'teams': { 
            $elemMatch: {
                          'members' : { 
                                      $elemMatch : 
                                          { 
                                            'id' : Meteor.userId() 
                                          }  
                                      }
                        }
        }
})

I'd like to find the correct query to return all Boards within the Boards collection where the member id matches 1.

Could this be because you are storing id as string in the collection? As you will see below I tried your query replacing Meteor.userId() with a hardcoded string value (with single or double quotes) like '1' or '3' and I believe the query output is what you are looking for.


> db.boards.find().pretty()
{
        "_id" : ObjectId("567443e7fba7a186bcd48bfd"),
        "name" : "Board One",
        "teams" : [
                {
                        "name" : "Team One",
                        "members" : [
                                {
                                        "id" : "1",
                                        "name" : "Garrett"
                                },
                                {
                                        "id" : "2",
                                        "name" : "Sarah"
                                }
                        ]
                },
                {
                        "name" : "Team Two",
                        "members" : [
                                {
                                        "id" : "1",
                                        "name" : "Garrett"
                                },
                                {
                                        "id" : "2",
                                        "name" : "Jeff"
                                }
                        ]
                }
        ]
}
{
        "_id" : ObjectId("5674441ffba7a186bcd48bfe"),
        "name" : "Board Two",
        "teams" : [
                {
                        "name" : "Team One",
                        "members" : [
                                {
                                        "id" : "1",
                                        "name" : "Garrett"
                                },
                                {
                                        "id" : "2",
                                        "name" : "Sarah"
                                }
                        ]
                },
                {
                        "name" : "Team Three",
                        "members" : [
                                {
                                        "id" : "1",
                                        "name" : "Jim"
                                },
                                {
                                        "id" : "2",
                                        "name" : "Samson"
                                }
                        ]
                }
        ]
}
>

> db.boards.find({
...   'teams': {
...             $elemMatch: {
...                           'members' : {
...                                       $elemMatch :
...                                           {
...                                             'id' : '1'
...                                           }
...                                       }
...                         }
...         }
... })
{ "_id" : ObjectId("567443e7fba7a186bcd48bfd"), "name" : "Board One", "teams" : [ { "name" : "Team One", "members" : [ { "id" : "1", "name" : "Garrett" }, { "id" : "2", "name" : "Sarah" } ] }, { "name" : "Team Two", "members" : [ { "id" : "1", "name" : "Garrett" }, { "id" : "2", "name" : "Jeff" } ] } ] }
{ "_id" : ObjectId("5674441ffba7a186bcd48bfe"), "name" : "Board Two", "teams" : [ { "name" : "Team One", "members" : [ { "id" : "1", "name" : "Garrett" }, { "id" : "2", "name" : "Sarah" } ] }, { "name" : "Team Three", "members" : [ { "id" : "1", "name" : "Jim" }, { "id" : "2", "name" : "Samson" } ] } ] }
>
>
> db.boards.find({
...   'teams': {
...             $elemMatch: {
...                           'members' : {
...                                       $elemMatch :
...                                           {
...                                             'id' : '3'
...                                           }
...                                       }
...                         }
...         }
... })
>
>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM