【问题标题】:Mongodb $mergeObjects use variable attribute as object keyMongodb $mergeObjects 使用变量属性作为对象键
【发布时间】:2019-08-27 22:00:04
【问题描述】:

我目前正在编写一个聚合并有一个名为 tags 的复杂对象数组,例如:{name: 'tag1'}

我想确保数组只包含唯一的tags

    {
        $addFields: {
            uniqueTags: {
                $reduce: {
                    input: "$tags",
                    initialValue: {},
                    in: {$mergeObjects: {"$$this.name": "$$this"}}
        }}
    }},

上面的代码失败,因为我使用$$this.name作为对象键。如果我将其更改为其他内容,例如5,聚合将完美运行。

【问题讨论】:

  • 你能展示示例文档和你想要的输出吗

标签: mongodb mongoose mongoid


【解决方案1】:

$addToSet 适用于嵌套对象。

【讨论】:

    猜你喜欢
    • 2022-06-23
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    • 2019-09-25
    • 2010-11-28
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多