【发布时间】:2018-08-24 03:57:03
【问题描述】:
我想使用聚合功能将文档数组扁平化为键控对象。这是我的文档示例:
[
{
"_id": 1,
"texts": [
{ "language": "english", "text": "hello" },
{ "language": "german", "text": "hallo" },
{ "language": "french", "text": "bonjour" }
]
}, …
]
预期结果:
[
{
"_id": 1,
"texts": {
"english": "hello",
"german": "hallo",
"french": "bonjour"
}
}, …
]
我研究过不同的运算符,例如$map,但这似乎集中在将数组转换为数组。我可能需要一个等效于 $reduce 的 JS,但找不到将我的值累积到对象中的方法。
有什么想法吗?
【问题讨论】:
标签: mongodb aggregation-framework