Mongodb array $push and $pull

In case you need replace one array value to another, you can use arrayFilters for update.

(at least, present in mongo 4.2.1).

db.your_collection.update(
    { "_id": ObjectId("your_24_byte_length_id") },
    { "$set": { "profile.experience.$[elem]": "new_value" } },
    { "arrayFilters": [ { "elem": { "$eq": "old_value" } } ], "multi": true }
) 

This will replace all "old_value" array elements with "new_value".


I found this explanation:

The issue is that MongoDB doesn’t allow multiple operations on the same property in the same update call. This means that the two operations must happen in two individually atomic operations.

And you can read that posts:

Pull and addtoset at the same time with mongo

multiple mongo update operator in a single statement?

Tags:

Mongodb