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

MongodbUpsert

Mongodb Problem Overview


This error happens when I tried to update upsert item:

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

Mongodb Solutions


Solution 1 - Mongodb

Field should appear either in $set, or in $setOnInsert. Not in both.

Solution 2 - Mongodb

I had the same problem while performing an update query using PyMongo.
I was trying to do:


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

Notice that here I'm trying to update the value of key1 from two MongoDB Update Operators.

This basically happens when you try to update the value of a same key with more than one MongoDB Update Operators within the same query.

You can find a list of Update Operators over here

Solution 3 - Mongodb

If you pass the same key in $set and in $unset when updating an item, you will get that error.

For example:

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'

Solution 4 - Mongodb

You cannot have the same path referenced more than once in an update. For example, even though the below would result in something logical, MongoDB will not allow it.

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

You would get the following error:

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

Solution 5 - Mongodb

db.products.update(
  { _id: 1 },
  {
     $set: { item: "apple" },
     $setOnInsert: { defaultQty: 100 }
  },
  { upsert: true }
)

Below is the key explanation to the issue: > MongoDB creates a new document with _id equal to 1 from the > condition, and then applies the $set AND $setOnInsert operations to > this document.

If you want a field value is set or updated regardless of insertion or update, use it in $set. If you want it to be set only on insertion, use it in $setOnInsert.

Here is the example: https://docs.mongodb.com/manual/reference/operator/update/setOnInsert/#example

Solution 6 - Mongodb

Starting from MongoDB 4.2 you can use aggregate pipelines in update:

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() also supports it

Solution 7 - Mongodb

I recently had the same issue while using the query below.

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

When i have changed 'content.note' to 'content.$.note' it has been fixed. So my final query is :

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

Solution 8 - Mongodb

With the Ruby library at least, it's possible to get this error if you have the same key twice, once as a symbol and once as a string:

db.getCollection("user").updateOne(
  {_id: ...},
  {$set: {'name': "Horse", name: "Horse"}}
)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionzoredView Question on Stackoverflow
Solution 1 - MongodbzoredView Answer on Stackoverflow
Solution 2 - MongodbsaintlyzeroView Answer on Stackoverflow
Solution 3 - MongodbRodrigoNOFXView Answer on Stackoverflow
Solution 4 - MongodbDavidView Answer on Stackoverflow
Solution 5 - MongodbOnur DemirView Answer on Stackoverflow
Solution 6 - MongodbAndrey HohutkinView Answer on Stackoverflow
Solution 7 - MongodbEnesView Answer on Stackoverflow
Solution 8 - MongodbhrdwdmrblView Answer on Stackoverflow