0

I am trying to do following update in mongo, and getting error:

db.xyz.updateOne(
    { "xyzId": 1 },
    {
        $push: { "packages": { "id": 11 } },
        $pull: { "packages.$[pkg10].items": { "id": 1000 } }

    },

    {
        arrayFilters: [{ "pkg10.id": 1 }]
    }
)

Error:

{
    "message" : "Updating the path 'packages.$[pkg10].items' would create a conflict at 'packages'",
    "stack" : "script:1:11",
    "name" : "WriteError",
    "code" : 40,
    "index" : 0
}

My question is why this is not able to first add one package in order and then remove item from another specified package ? And how can I know which kinds of updates can lead this type of error.

Similar Question,(but was not helpful): Updating the path 'x' would create a conflict at 'x'

dhanu10896
  • 315
  • 2
  • 16

1 Answers1

1

how can I know which kinds of updates can lead this type of error

If you try to update the same path i.e packages in the same update operation.

Here, you have pull and push on packages. Hence the error occurred. If you have to update another path or field, you can add it in the same update operation.

why this is not able to first add one package in order and then remove item from another specified package

It's a restriction/feature-missing. It's the way in mongo db.

Gibbs
  • 21,904
  • 13
  • 74
  • 138