【问题标题】:Need a match an array value in mongodb and append into that需要匹配 mongodb 中的数组值并附加到该数组中
【发布时间】:2021-12-13 15:57:58
【问题描述】:
{
    "_id": "100",
    "menu": [
        {
            "type": "1",
            "isenabled": true,
            "items": [
                {
                    "key": "activity",
                    "enabled": true,
                    "criteria": [
                        {
                            "key": "account",
                            "value": [
                                "active"
                            ],
                            "isExclude": false
                        }
                    ],
                    "eligible": [
                        "QA",
                        "DE"
                    ],
                    "isItem": false
                }
            ]
        }
}

我在一个集合中有以下类型的文档。 在这里,我想确定符合条件的,比如只有“QA”、“DE”的地方,我们需要添加一些额外的价值。 我们如何才能一次在多个文档中做到这一点。

"eligible": [
                "QA",
                "DE"
             ]

【问题讨论】:

  • 你的预期结果是什么

标签: arrays mongodb mongoose mongodb-query


【解决方案1】:

使用$push

db.collection.update({},
{
  $push: {
    "menu.$[m].items.$[i].eligible": "US"
  }
},
{
  arrayFilters: [
    {
      "m.type": "1"
    },
    {
      "i.eligible": [
        "QA",
        "DE"
      ]
    }
  ],
  multi: true
})

mongoplayground

【讨论】:

  • 非常感谢于婷。它工作正常。我尝试了以下方法来添加多个数据时间,它也可以按预期工作。 "menu.$[m].items.$[i].eligible": { $each: [ "US", "WQ", "ER" ]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-17
  • 2020-05-31
  • 2018-08-03
  • 1970-01-01
  • 2019-08-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多