【发布时间】:2019-08-27 22:00:04
【问题描述】:
我目前正在编写一个聚合并有一个名为 tags 的复杂对象数组,例如:{name: 'tag1'}。
我想确保数组只包含唯一的tags。
{
$addFields: {
uniqueTags: {
$reduce: {
input: "$tags",
initialValue: {},
in: {$mergeObjects: {"$$this.name": "$$this"}}
}}
}},
上面的代码失败,因为我使用$$this.name作为对象键。如果我将其更改为其他内容,例如5,聚合将完美运行。
【问题讨论】:
-
你能展示示例文档和你想要的输出吗