mongodb - 更新嵌套数组的字段时,如何使 mongodb 的 "updated"停止?

我有这样一个数据库:

  {
        "universe":"comics",
        "saga":[
           {
              "name":"x-men",
              "characters":[
                 {
                    "character":"wolverine",
                    "picture":"618035022351.png"
                 },
                 {
                    "character":"wolverine",
                    "picture":"618035022352.png"
                 }
              ]
           }
        ]
     },
     {
        "universe":"dc",
        "saga":[
           {
              "name":"spiderman",
              "characters":[
                 {
                    "character":"venom",
                    "picture":"618035022353.png"
                 }
              ]
           }
        ]
     }

使用这段代码,我更新了 name: wolverine 的字段:

db.getCollection('collection').findOneAndUpdate(
  {
    "universe": "comics"
  },
  {
    $set: {
      "saga.$[outer].characters.$[inner].character": "lobezno",
      "saga.$[outer].characters.$[inner].picture": "618035022354.png"
    }
  },
  /*{
    "saga.characters": 1
  },*/
  {
    "arrayFilters": [
      {
        "outer.name": "x-men"
      },
      {
        "inner.character": "wolverine"
      }
    ],
      "multi":false
  }

)

我只想更新第一个匹配的对象,然后停止它。

例如,如果我有一个包含 100,000 个元素的数组,并且匹配所在的对象位于第十个位置,他将更新该记录,但他将继续遍历整个数组,这对我来说似乎无效尽管他已经进行了更新。

注意:如果我使用 universe.saga.characters 中的 _id 进行更新,而不是使用 name 进行更新,它仍然会遍历其余元素。

我该怎么做?

最佳答案

使用 arrayFilters 更新条件

我不认为它会通过循环查找和更新,如果集合有 100,000 个子文档也没有关系,因为这里在 $[<identifier>] 中有很好的解释。并提到:

  • $[<identifier>]定义一个标识符以仅更新与 arrayFilters 中相应过滤文档匹配的那些数组元素

  • 在更新文档中,使用$[<identifier>] filtered 位置运算符定义一个标识符,然后您可以在数组过滤器文档中引用它。但是,如果标识符未包含在更新文档中,请确保您不能拥有标识符的数组过滤器文档。

使用 _id 更新

你的观点,

Note: if I did the update using an _id inside of universe.saga.characters instead of doing the update using the name, it would still loop through the rest of the elements.

MongoDB 肯定会使用 _id指数。这是不错的 answer有问题MongoDB Update Query Performance , 从这里你会更好地理解以上几点

使用索引字段更新

您可以根据更新命令的查询部分创建索引,这里MongoDB Indexes和 Indexing Strategies已经解释了为什么索引很重要,

在你的例子中,让我们看例子:

Example 1: If document have 2 sub documents and when you update and check with explain("executionStats"), assume it will take 1 second to update,

quick use Mongo Playground (this platform will not support update query)

Example 2: If document have 1000 sub documents and when you update and check with explain("executionStats"), might be it will take more then 1 second,

If provide index on fields (universe, saga.characters.character and saga.characters.picture) then definitely it will take less time then usual without index, main benefit of index it will direct point to indexed fields.

quick use Mongo Playground (this platform will not support update query)

为您的字段创建索引

db.maxData.createIndex({ 
    "universe": 1, 
    "saga.characters.character": 1, 
    "saga.characters.picture": 1
})

For more experiment use above 2 examples data with index and without index and check executionStats you will get more clarity.

关于mongodb - 更新嵌套数组的字段时,如何使 mongodb 的 "updated"停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63210406/

相关文章:

python - 多交易所、多币种、多金额的套利算法

macos - 没有应用程序包的 QT 命令行可执行二进制文件有 Macdeployqt 吗?

postgresql - 如何从手机访问 postgreSQL 数据库?

java - 在 Java 中使用 OpenPDF 居中/对齐表格单元格中的文本

docker - 公共(public) GKE 集群中的 pod 无法访问互联网

python - Python的Tkinter模块如何让对话框窗口出现在最前面?

shopify - 如何使用 Shopify API 更新电子邮件模板(通知)?

reactjs - React 应用程序部署未定义 Netlify 环境变量

python - 如何在不返回并更改数据的情况下更改 seaborn fiddle 图的 x 轴标签

flutter - Firestore 复合查询不适用于 Flutter Web