【问题标题】:Updating the path 'x' would create a conflict at 'x'更新路径“x”会在“x”处产生冲突
【发布时间】:2018-11-29 13:27:25
【问题描述】:

当我尝试更新 upsert 项目时发生此错误:

Updating the path 'x' would create a conflict at 'x'

【问题讨论】:

    标签: mongodb upsert


    【解决方案1】:

    字段应出现在$set$setOnInsert 中。两者都没有。

    【讨论】:

    • 这没有意义。如果找到文档,则发生“$set”,如果找不到文档,则发生“$setOnInsert”。没有冲突,因为两者永远不会同时应用。如果你不能在同一个领域同时使用两者,那就违背了目的,不是吗?
    • $set 如果找到文档并且如果未找到则发生。因此它在$setOnInsert{upsert: true} 的“未找到”状态下发生冲突。似乎 MongoDB 开发人员无法定义一个优先级:)
    • 是的,我意识到了。确实应该有一个“$setOnUpdate”运算符。
    • 作为mongodb文档,对这个文档应用$set和$setOnInsert操作。 docs.mongodb.com/manual/reference/operator/update/setOnInsert
    • 至少错误信息可以更好
    【解决方案2】:

    我在使用 PyMongo 执行 update 查询时遇到了同样的问题。
    我正在尝试做:

    
    > db.people.update( {'name':'lmn'}, { $inc : { 'key1' : 2 }, $set: { 'key1' : 5 }})
    

    请注意,这里我尝试从两个 MongoDB 更新运算符 更新 key1 的值。

    当您尝试在同一查询中使用多个 MongoDB 更新运算符 更新 same key 的值时,基本上会发生这种情况。

    您可以通过here 找到更新运算符列表

    【讨论】:

      【解决方案3】:

      如果您在更新项目时在$set$unset 中传递相同的密钥,则会收到该错误。

      例如:

      const body = {
         _id: '47b82d36f33ad21b90'
         name: 'John',
         lastName: 'Smith'
      }
      
      MyModel.findByIdAndUpdate(body._id, { $set: body, $unset: {name: 1}})
      
      // Updating the path 'name' would create a conflict at 'name'
      

      【讨论】:

        【解决方案4】:

        您不能在更新中多次引用同一路径。例如,即使下面会产生一些合乎逻辑的结果,MongoDB 也不会允许它。

        db.getCollection("user").updateOne(
          {_id: ...},
          {$set: {'address': {state: 'CA'}, 'address.city' : 'San Diego'}}
        )
        

        你会得到以下错误:

        Updating the path 'address.city' would create a conflict at 'address'
        

        【讨论】:

          【解决方案5】:
          db.products.update(
            { _id: 1 },
            {
               $set: { item: "apple" },
               $setOnInsert: { defaultQty: 100 }
            },
            { upsert: true }
          )
          

          以下是该问题的关键解释:

          MongoDB 创建一个 _id 等于 1 的新文档 条件,然后 将 $set AND $setOnInsert 操作应用于 这份文件。

          如果您想设置或更新字段值而不考虑插入或更新,请在 $set 中使用它。如果您希望它仅在插入时设置,请在 $setOnInsert 中使用它。

          示例如下:https://docs.mongodb.com/manual/reference/operator/update/setOnInsert/#example

          【讨论】:

          • 对我来说,问题是两个运算符中都出现了相同的字段(“dateUpdate”)。我只在 $set 中留下了“dateUpdate”并解决了我的问题。
          【解决方案6】:

          从 MongoDB 4.2 开始,您可以在更新中使用聚合管道:

          db.your_collection.update({
              _id: 1
          }, 
          [{
              $set:{
                  x_field: {
                      $cond: {
                          if: {$eq:[{$type:"$_id"} ,  "missing"]},
                          then: 'upsert value',   // it's the upsert case
                          else: '$x_field'    // it's the update case
                      }
                  }
              }
          }],
          {
               upsert: true
          })
          

          db.collection.bulkWrite() 也支持

          【讨论】:

            【解决方案7】:

            我最近在使用下面的查询时遇到了同样的问题。

            TextContainer.findOneAndUpdate({ blockId: req.params.blockId, 'content._id': req.params.noteId }, { $set: { 'content.note': req.body.note } }, { upsert: true, new: true })
            

            当我将 'content.note' 更改为 'content.$.note' 时,它已被修复。所以我的最终查询是:

            TextContainer.findOneAndUpdate({ blockId: req.params.blockId, 'content._id': req.params.noteId }, { $set: { 'content.$.note': req.body.note } }, { upsert: true, new: true })
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-02-28
              • 1970-01-01
              • 2015-02-23
              相关资源
              最近更新 更多