我很难得到这个结果:
["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,但由于我没有找到新手友好的文档,因此无法使其正常工作。
非常感谢您的解决方案和友好的解释!
答案 0 :(得分:0)
非常确定使用标准Meteor无法做到这一点。聚合包可能会有所帮助,但我也没有使用它。我使用方法1来检索文档,然后过滤文档以使用标准javascript找到所需的信息(或类似下划线的内容可能会帮助你。)
答案 1 :(得分:0)
在这里使用聚合似乎有点矫枉过正。这是一个提取插座名称的示例函数:
var findOutletName = function(name, city) {
var doc = Stores.findOne({name: name});
var outletName = null;
_.each(doc.storeLocation, function(location) {
_.each(location.outlet, function(outlet) {
if (outlet.city === city)
outletName = outlet.name;
});
});
return outletName;
};
您可以这样使用:
findOutletName('Soda Rock', 'Melbourne');