【发布时间】:2015-09-16 23:33:20
【问题描述】:
我很难得到这个结果:
["Chadstone", "South Yarra"]
当给定这两个值时:
name: 'Soda Rock'
city: 'Melbourne'
来自本文档:
{
"_id" : "axHqB4NjXbWwphik3",
"name" : "Soda Rock",
"storeLocation" : [
{
"state" : "Victoria",
"outlet" : [
{
"city" : "Melbourne",
"name" : ["Chadstone", "South Yarra"]
},
{
"city" : "Geelong",
"name" : ["Myer", "Market Square"]
}
]
},
{
"state" : "New South Wales",
"outlet" : [
{
"city" : "Sydney",
"name" : ["Westfield", "Broadway Shopping Centre"]
}
]
}
]
}
我尝试了几种方法,但都没有输出我想要的结果。
方法#1:
Stores.find(
{
'name': 'Soda Rock',
'storeLocation.outlet.city': 'Melbourne'
},
{
_id: 0,
'storeLocation.outlet.name.$': 1
}
);
结果:
{
"storeLocation" : [
{
"state" : "Victoria",
"outlet" : [
{
"city" : "Melbourne",
"name" : ["Chadstone", "South Yarra"]
},
{
"city" : "Geelong",
"name" : ["Myer", "Market Square"]
}
]
}
]
}
方法#2:
Stores.find(
{
'name': 'Soda Rock',
'storeLocation.outlet.city': {
'Melbourne'
}
},
{
_id: 0,
'storeLocation.outlet.name': 1
}
);
结果:
{
"storeLocation" : [
{
"outlet" : [
{
"name" : ["Chadstone", "South Yarra"]
},
{
"name" : ["Myer", "Market Square"]
}
]
},
{
"outlet" : [
{
"name" : ["Westfield", "Broadway Shopping Centre"]
}
]
}
]
}
我也尝试过使用meteorhacks:aggregate,但由于我没有找到对新手友好的文档,所以无法使用。
非常感谢您的解决方案和友好的解释!
【问题讨论】:
标签: javascript mongodb meteor