Mongo filter/find 嵌入数组中符合条件的所有元素和 return 周围文档?
Mongo filter/find all elements in embedded array that match a criteria and return surrounding documents?
基本上,我有一个具有以下结构的 Mongo 数据库:
[
{
"id": 1,
"other_thing": "whatever",
"annoying_array": [
{
"sub_id": "a",
"thing_type": "apple"
},
{
"sub_id": "b",
"thing_type": "pear"
}
]
},
{
"id": 2,
"other_thing": "whatever_else",
"annoying_array": [
{
"sub_id": "c",
"thing_type": "carrot"
},
{
"sub_id": "d",
"thing_type": "pear"
},
{
"sub_id": "e",
"thing_type": "pear"
}
]
}
]
我基本上想做类似 db.docs.find( {annoying.array.thing_type: "pear"})
的事情并拥有它 return:
[
{
"id": 1,
"other_thing": "whatever",
"annoying_array": [
{
"sub_id": "b",
"thing_type": "pear"
}
]
},
{
"id": 2,
"other_thing": "whatever_else",
"annoying_array": [
{
"sub_id": "d",
"thing_type": "pear"
},
{
"sub_id": "e",
"thing_type": "pear"
}
]
}
]
This question 似乎相关,但已经有将近 10 年的历史了,而且我知道 filter/aggregation 管道从那时起已经发生了很大变化。可能有更简洁的方法。
我已经尝试了我能想到的一切,从 find
到 match
和 elemMatch
但所有 return 都是文档和子数组 存在 thing_type: 'pear'
,并非 thing_type = pear
的子数组中的每个元素。即使它没有 return 周围的元数据(即不包括 other_thing
等),我也会满足于只 returning 每个元素(子-document) 其键匹配特定值.
如果可以选择 return annoying_array
的每个匹配元素作为其自己的文档匹配,以及文档范围的数据(即 id
, 或 other_thing
) 投影到每场比赛。
您可以使用 Aggregation Pipeline
:
$match
查找 annoying_array
包含 thing_type
属性 等于 pear
. 的项目的所有文档
$project
指定要从结果return 中获取哪些属性
$filter
仅过滤 annoying_array
中 thing_type
属性 等于 pear
. 的项目
db.collection.aggregate([
{
"$match": {
"annoying_array.thing_type": "pear"
}
},
{
"$project": {
"id": 1,
"other_thing": 1,
"annoying_array": {
"$filter": {
input: "$annoying_array",
as: "item",
cond: {
"$eq": [
"$$item.thing_type",
"pear"
]
}
}
}
}
}
])
基本上,我有一个具有以下结构的 Mongo 数据库:
[
{
"id": 1,
"other_thing": "whatever",
"annoying_array": [
{
"sub_id": "a",
"thing_type": "apple"
},
{
"sub_id": "b",
"thing_type": "pear"
}
]
},
{
"id": 2,
"other_thing": "whatever_else",
"annoying_array": [
{
"sub_id": "c",
"thing_type": "carrot"
},
{
"sub_id": "d",
"thing_type": "pear"
},
{
"sub_id": "e",
"thing_type": "pear"
}
]
}
]
我基本上想做类似 db.docs.find( {annoying.array.thing_type: "pear"})
的事情并拥有它 return:
[
{
"id": 1,
"other_thing": "whatever",
"annoying_array": [
{
"sub_id": "b",
"thing_type": "pear"
}
]
},
{
"id": 2,
"other_thing": "whatever_else",
"annoying_array": [
{
"sub_id": "d",
"thing_type": "pear"
},
{
"sub_id": "e",
"thing_type": "pear"
}
]
}
]
This question 似乎相关,但已经有将近 10 年的历史了,而且我知道 filter/aggregation 管道从那时起已经发生了很大变化。可能有更简洁的方法。
我已经尝试了我能想到的一切,从 find
到 match
和 elemMatch
但所有 return 都是文档和子数组 存在 thing_type: 'pear'
,并非 thing_type = pear
的子数组中的每个元素。即使它没有 return 周围的元数据(即不包括 other_thing
等),我也会满足于只 returning 每个元素(子-document) 其键匹配特定值.
如果可以选择 return annoying_array
的每个匹配元素作为其自己的文档匹配,以及文档范围的数据(即 id
, 或 other_thing
) 投影到每场比赛。
您可以使用 Aggregation Pipeline
:
$match
查找annoying_array
包含thing_type
属性 等于pear
. 的项目的所有文档
$project
指定要从结果return 中获取哪些属性$filter
仅过滤annoying_array
中thing_type
属性 等于pear
. 的项目
db.collection.aggregate([
{
"$match": {
"annoying_array.thing_type": "pear"
}
},
{
"$project": {
"id": 1,
"other_thing": 1,
"annoying_array": {
"$filter": {
input: "$annoying_array",
as: "item",
cond: {
"$eq": [
"$$item.thing_type",
"pear"
]
}
}
}
}
}
])