mongodb-如何在嵌套数组中查找和更新元素

发布于 2025-02-06 05:29:59 字数 1260 浏览 2 评论 0原文

这是集合:

db.employees.insertMany([
   {
    "data": {
      "category": [
        {
          "name": "HELLO",
          "subcategory": [
            "EDUCATION",
            "ART",
            
          ]
        },
         {
          "name": "HELLO",
          "subcategory": [
            "GG",
            "ART",
            
          ]
        },
        {
          "name": "HELLO",
          "subcategory": [
            "EDUCATION",
            "SHORE",
            
          ]
        }
      ]
    }
  },
  {
    "data": {
      "category": [
        {
          "name": "HELLO",
          "subcategory": [
            "EDUCATION",
            "HELLO",
            
          ]
        }
      ]
    }
  },
  {
    "data": {
      "category": [
        {
          "name": "HELLO",
          "subcategory": [
            "GG",
            "ART",
            
          ]
        }
      ]
    }
  }
]);

我想要的是使用包含“教育”的“子类别”中的“类别”中的元素,然后用另一个字符串替换“教育”,例如“体育”。

我尝试了几个命令,但没有真正完成这项工作:

db.employees.updateMany({
  "data.category.subcategory": "EDUCATION"
},
{
  "$set": {
    "data.category.$": {
      "subcategory": "SPORTS"
    }
  }
})

我看到的是它不会通过更换元素来更新该元素,并且不能替换每个符合条件的元素。

Here is the collection:

db.employees.insertMany([
   {
    "data": {
      "category": [
        {
          "name": "HELLO",
          "subcategory": [
            "EDUCATION",
            "ART",
            
          ]
        },
         {
          "name": "HELLO",
          "subcategory": [
            "GG",
            "ART",
            
          ]
        },
        {
          "name": "HELLO",
          "subcategory": [
            "EDUCATION",
            "SHORE",
            
          ]
        }
      ]
    }
  },
  {
    "data": {
      "category": [
        {
          "name": "HELLO",
          "subcategory": [
            "EDUCATION",
            "HELLO",
            
          ]
        }
      ]
    }
  },
  {
    "data": {
      "category": [
        {
          "name": "HELLO",
          "subcategory": [
            "GG",
            "ART",
            
          ]
        }
      ]
    }
  }
]);

What I want is to locate the elements in 'category' with a 'subcategory' that contains 'EDUCATION' and replace 'EDUCATION' with another string, let's say 'SPORTS'.

I tried a couple of commands but nothing really did the job:

db.employees.updateMany({
  "data.category.subcategory": "EDUCATION"
},
{
  "$set": {
    "data.category.
quot;: {
      "subcategory": "SPORTS"
    }
  }
})

What I saw is that it doesn't update the element by replacing it and it doesn't replace every element that meets the criteria.

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

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

发布评论

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

评论(2

提赋 2025-02-13 05:29:59

想想 mongongodb更新带有聚合管道满足您的场景。

  1. $ set - set data.category value。

    1.1。 $ map - 迭代data.Category中的每个元素并返回数组。

    1.1.1。 $ MergeObjects - 将当前文档与sub类别字段合并, 1.1.1.1.1 。。

    1.1.1.1 $ map - 从subcategory数组中迭代每个值。使用$ cond教育sports替换,请使用现有值($$ this this) 。

db.employees.updateMany({
  "data.category.subcategory": "EDUCATION"
},
[
  {
    "$set": {
      "data.category": {
        $map: {
          input: "$data.category",
          in: {
            $mergeObjects: [
              "$this",
              {
                subcategory: {
                  $map: {
                    input: "$this.subcategory",
                    in: {
                      $cond: {
                        if: {
                          $eq: [
                            "$this",
                            "EDUCATION"
                          ]
                        },
                        then: "SPORTS",
                        else: "$this"
                      }
                    }
                  }
                }
              }
            ]
          }
        }
      }
    }
  }
]

示例mongo playground

Think that MongoDB Update with Aggregation Pipeline fulfills your scenario.

  1. $set - Set data.category value.

    1.1. $map - Iterate each element in data.category and return an array.

    1.1.1. $mergeObjects - Merge the current document with the document with subcategory field from 1.1.1.1.

    1.1.1.1 $map - Iterate each value from the subcategory array. With $cond to replace the word EDUCATION with SPORTS if fulfilled, else use existing value ($$this).

db.employees.updateMany({
  "data.category.subcategory": "EDUCATION"
},
[
  {
    "$set": {
      "data.category": {
        $map: {
          input: "$data.category",
          in: {
            $mergeObjects: [
              "$this",
              {
                subcategory: {
                  $map: {
                    input: "$this.subcategory",
                    in: {
                      $cond: {
                        if: {
                          $eq: [
                            "$this",
                            "EDUCATION"
                          ]
                        },
                        then: "SPORTS",
                        else: "$this"
                      }
                    }
                  }
                }
              }
            ]
          }
        }
      }
    }
  }
]

Sample Mongo Playground

倾城花音 2025-02-13 05:29:59

这是使用“ ArrayFilters”进行操作的另一种方法。

db.collection.update({
  "data.category.subcategory": "EDUCATION"
},
{
  "$set": {
    "data.category.$[].subcategory.$[elem]": "SPORTS"
  }
},
{
  "arrayFilters": [
    { "elem": "EDUCATION" }
  ],
  "multi": true
})

尝试一下 mongoplayground.net.net

Here's another way to do it using "arrayFilters".

db.collection.update({
  "data.category.subcategory": "EDUCATION"
},
{
  "$set": {
    "data.category.$[].subcategory.$[elem]": "SPORTS"
  }
},
{
  "arrayFilters": [
    { "elem": "EDUCATION" }
  ],
  "multi": true
})

Try it on mongoplayground.net.

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