javascript - 连接集合中元素的所有数组 [MongoDB]
问题描述
抱歉,我没有很好地了解 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 聚合,性能会改变吗?
解决方案
聚合总是一个更好的选择,而不是使用一些语言代码,这就是为什么数据库提供这种类型的救济来一次性获得结果。
db.collection.aggregate([
{ "$group": {
"_id": null,
"data": { "$push": "$array" }
}},
{ "$project": {
"_id": 0,
"data": {
"$reduce": {
"input": "$data",
"initialValue": [],
"in": { "$concatArrays": ["$$this", "$$value"] }
}
}
}}
])
您唯一需要注意的是单个文档的返回结果的大小不应超过 16MB Bson 限制。你可以从这里学到更多
推荐阅读
- html - 角度 src 图像的延迟加载
- c# - 如何以度数获得对象旋转?统一
- mongodb - MongoDB 中的 UpdateMany 使用 $inc 运行两次
- node.js - 应用程序级别的 Kubernetes 证书?
- r - 如何使用 inst/extdata 中的文件?R 包检查防止在 R 3.6 中使用 system.file()
- javascript - 获取后“TypeError:无法读取未定义的属性'forEach'”
- python - 当我知道密钥存在时如何处理 Python (JSON) 密钥错误?
- asp.net-mvc - 未找到 MSBuild 发布配置文件 .pubxml.user 文件
- javascript - Javascript - Puppeteer - 文本查找
- vaticle-typedb - 有没有办法在 .gql 文件中编写插入 GRAQL 语句并在 GRAKN 控制台或 Workbase 中处理它?