Updating the path 'x' would create a conflict at 'x'
Asked Answered
S

9

106

This error happens when I tried to update upsert item:

Updating the path 'x' would create a conflict at 'x'
Sparkie answered 20/6, 2018 at 12:1 Comment(0)
S
95

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

Sparkie answered 20/6, 2018 at 12:1 Comment(8)
This doesn't make sense. "$set" happens if the document is found, "$setOnInsert" if the document is not found. There's no conflict since both will never be applied at the same time. If you can't use both on the same field, that kind of defeats the purpose, no?Gatha
$set happens if the document is found and if it's not found. Therefore it conflicts in "not found" state on $setOnInsert and {upsert: true}. Seems like MongoDB developers could not define priority of one over another :)Sparkie
Yeah, I realized that. There should really be a "$setOnUpdate" operator.Gatha
As the mongodb document, it applies the $set and $setOnInsert operations to this document. docs.mongodb.com/manual/reference/operator/update/setOnInsertMarnimarnia
Just yeah, don't duplicate fields. No one have mentioned that, but $setOnInsert is actually adding those fields to update doc. Just specify which fields you want to be inserted on insert and update doc excluding properties from insert doc.Albric
at least the error message could have been betterGilding
But what if I want to insert a document if it doesn't exist, and update just one field if it exists? Both must have the x field.Ism
@Ism at lease in javascript you can do $set:{x: defaultValue, ...data} without the $setOnInsert, so if x is in data then it would override the default value.Barrada
Z
19

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

Zerla answered 16/9, 2019 at 10:2 Comment(0)
Y
15

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'
Ynez answered 4/2, 2021 at 19:26 Comment(0)
T
13

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'
Tympanites answered 11/6, 2019 at 14:25 Comment(0)
B
11
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

Borrero answered 24/6, 2020 at 11:54 Comment(2)
For me the issue was that same field ("dateUpdate") appeared in both operators. I left "dateUpdate" only in $set and solved my problem.Hainaut
WTF - thanks for pointing this out.Mastrianni
S
3

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

Soble answered 9/7, 2020 at 16:47 Comment(2)
how is your example a aggregate pipelines? please explain?Mindful
db.collection.bulkWrite() worksMindful
A
2

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"}}
)
Annaleeannaliese answered 3/5, 2022 at 18:45 Comment(0)
K
1

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 })
Kos answered 13/10, 2021 at 12:19 Comment(0)
A
1

I observed that there is no direct solution mentioned here for this error. In my case, I was removing an ID from my schema list and also adding a new ID at the same time and got this error.

Earlier code :

const findProject = await ProjectSchema.findOneAndUpdate(
        { projectId },
        { 
          $addToSet: { projectPolicyList: policyId },
          $pull: {projectPolicyList: oldPolicyId} 
        }
    );

So, a simple workaround for this is to update the list one at a time. In my case, I first added new element and then deleted from the list.

Correct Code :

const pushProjectPolicyList = await ProjectSchema.findByIdAndUpdate(projectId,
  { $addToSet: { projectPolicyList: policyId } });

const pullProjectPolicyList = await ProjectSchema.findByIdAndUpdate(projectId,
  { $pull: { projectPolicyList: oldPolicyId } });
Allisan answered 11/2, 2024 at 13:36 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.