抱歉,我没有很好地获取MongoDB聚合。我如何通过聚合实现这一点:
[
{array: [1,2,3] },
{array: [4,5,6] },
{array: [7,8,9] }
]
desired result:
[1,2,3,4,5,6,7,8,9]
如果我将文档视为普通对象,而不是使用MongoDB聚合,性能会发生变化吗?
发布于 2019-06-05 03:32:26
您可以使用$group by null
来将数组数组作为单个文档来获取,然后可以使用$concatArrays运行$reduce来扁平化该数组:
db.col.aggregate([
{
$group: {
_id: null,
array: { $push: "$array" }
}
},
{
$project: {
_id: 0,
array: {
$reduce: {
input: "$array",
initialValue: [],
in: { $concatArrays: [ "$$value", "$$this" ] }
}
}
}
}
])
https://stackoverflow.com/questions/56450138
复制相似问题