我想找到正确的查询来返回会员ID匹配的Boards集合中的所有董事会。任何接受者?
我的架构如下:
在“董事会”系列中,
{ 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' }
]
}
]
我正在尝试在集合中查询对象数组内的数组中的对象。我在以下查询中尝试了很多变化..
Boards.find({
'teams': {
$elemMatch: {
'members' : {
$elemMatch :
{
'id' : Meteor.userId()
}
}
}
}
})
我想找到正确的查询来返回会员ID与1匹配的Boards集合中的所有Boards。
答案 0 :(得分:2)
这可能是因为你将id作为字符串存储在集合中吗?正如您将在下面看到的,我尝试使用硬编码的字符串值(使用单引号或双引号)替换Meteor.userId(),例如' 1'或者' 3'我相信查询输出就是你要找的。 p>
> 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'
... }
... }
... }
... }
... })
>
>