【发布时间】:2015-06-30 22:34:37
【问题描述】:
首先,让我说这个问题与我在mongo 中看到的有关嵌套数组的其他问题略有不同。
他们中的大多数人询问如何在嵌套数组中获取特定元素。我想获取一个数组的所有元素,该数组本身具有另一个包含给定值的数组。
我有文件,看起来像这样:
{
_id: something,
value: something,
items: [{
name: someItem,
price: somePrice,
stores:[
{name: storeName, url: someUrl },
{name: anotherStoreName, url: someOtherUrl}
]
},
{
name: someOtherItem,
price: someOtherPrice,
stores:[
{name: storeName, url: someUrl },
{name: yetAnotherStoreName, url: someOtherUrl}
]
}]
}
我想要的是只获取在 stores 数组中具有给定商店的 items 元素。
也就是说,如果我问storeName,我应该得到示例中的两个项目。但是如果我要求anotherStoreName,我应该只得到数组项的第一个元素。
搜索类似问题并尝试解决方案我只能得到项目的第一个匹配元素,但我想要所有匹配元素。
我将不胜感激。
【问题讨论】:
-
你能告诉我们预期的结果吗?
标签: arrays mongodb mongodb-query