Skip to main content

Known Issues in MongoDB

using $push and $pull on the Same Field in a Single Operation​

MongoDB does not support applying both $push and $pull to the same field within a single update operation. This results in a write conflict error due to multiple update modifiers targeting the same path.

This behavior was observed in the past, where an operation attempting to both remove and insert into the same array field (nodes) in one update silently passed in our test environment but failed in production.

Example​

The following operation will raise an error in MongoDB:

await collection.update_one(
{"_id": 1},
{
"$pull": {"nodes": {"data.step_id": "abc"}},
"$push": {"nodes": {"data": {"step_id": "abc", ...}}}
}
)

Error (in real MongoDB):​

MongoWriteError: Updating the path 'nodes' would create a conflict

This is because both $pull and $push attempt to modify the nodes array in the same update.

Required Workaround​

These operations must be separated into two distinct update_one calls:

await collection.update_one(
{"_id": 1},
{"$pull": {"nodes": {"data.step_id": "abc"}}}
)

await collection.update_one(
{"_id": 1},
{"$push": {"nodes": new_node}}
)

MongoDB processes updates atomically per document, but not across conflicting field paths within a single update — so separating them is required.

Important Notes​

  • This issue is not enforced by mongomock or mongomock_motor, so tests using these mock will pass instead of fails.