mongodb-如何更新嵌套数组元素的特定属性

发布于 2025-01-25 21:06:20 字数 1035 浏览 0 评论 0 原文

我有一个带有以下结构的集合:

{
  arrangements: [
    { displayName: "MRT.8" },
    { displayName: "MRT.10" },
    { displayName: "MRT.12" },
    (...)
  ]
}

我希望substring MRT 被用 Mobile 替换,因此结果将如下:

{
  arrangements: [
    { displayName: "MOBILE.8" },
    { displayName: "MOBILE.10" },
    { displayName: "MOBILE.12" },
    (...)
  ]
}

plost 在SO 上解决类似问题的解决方案,我做了以下操作:

db.collection('releaseDocument').updateMany({"arrangements.displayName": {$regex: /MRT\..*/}}, [
      {
        $set: {
          'arrangements.displayName': {
            $concat: [
              "MOBILE.",
              {$arrayElemAt: [{$split: ["$displayName", "MRT."]}, 0]}
            ]
          }
        }
      }
    ])

但这是不起作用的:因为<代码> $ 是指当前文档,而不是嵌套数组元素。我该如何实现我上面描述的内容?

I have a collection with the following structure:

{
  arrangements: [
    { displayName: "MRT.8" },
    { displayName: "MRT.10" },
    { displayName: "MRT.12" },
    (...)
  ]
}

I want the substring MRT to be replaced with MOBILE, so the result will be as follows:

{
  arrangements: [
    { displayName: "MOBILE.8" },
    { displayName: "MOBILE.10" },
    { displayName: "MOBILE.12" },
    (...)
  ]
}

Following the solution for a similar problem on SO I did the following:

db.collection('releaseDocument').updateMany({"arrangements.displayName": {$regex: /MRT\..*/}}, [
      {
        $set: {
          'arrangements.displayName': {
            $concat: [
              "MOBILE.",
              {$arrayElemAt: [{$split: ["$displayName", "MRT."]}, 0]}
            ]
          }
        }
      }
    ])

But that doesn't work because $ refers to the current document, not the nested array element. How can I achieve what I described above?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

俯瞰星空 2025-02-01 21:06:20

看来您正在使用Contregation Pipeline进行更新,需要 $ MAP 操作员。

对于 $ arrayelementat 零件,我认为您需要1作为索引来获取第二个元素。

假设过滤器返回要更新的文档:

db.collection.update({
  "arrangements.displayName": {
    $regex: /MRT\..*/
  }
},
[
  {
    $set: {
      "arrangements": {
        $map: {
          input: "$arrangements",
          in: {
            $mergeObjects: [
              "$this",
              {
                displayName: {
                  $concat: [
                    "MOBILE.",
                    {
                      $arrayElemAt: [
                        {
                          $split: [
                            "$this.displayName",
                            "MRT."
                          ]
                        },
                        1
                      ]
                    }
                  ]
                }
              }
            ]
          }
        }
      }
    }
  }
])

示例mongo Playground

Seems you are working with Update with Aggregation Pipeline, you need $map operator.

For $arrayElementAt part, I think you need 1 as an index to take the second element.

Assume that the filter returns documents to be updated:

db.collection.update({
  "arrangements.displayName": {
    $regex: /MRT\..*/
  }
},
[
  {
    $set: {
      "arrangements": {
        $map: {
          input: "$arrangements",
          in: {
            $mergeObjects: [
              "$this",
              {
                displayName: {
                  $concat: [
                    "MOBILE.",
                    {
                      $arrayElemAt: [
                        {
                          $split: [
                            "$this.displayName",
                            "MRT."
                          ]
                        },
                        1
                      ]
                    }
                  ]
                }
              }
            ]
          }
        }
      }
    }
  }
])

Sample Mongo Playground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文