Mongodb array $push and $pull

后端 未结 2 1428
生来不讨喜
生来不讨喜 2020-12-10 10:54

I was looking to pull some value from array and simultaneously trying to update it.

    userSchema.statics.experience = function (id,xper,delet,callback) {
          


        
相关标签:
2条回答
  • 2020-12-10 11:44

    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?

    0 讨论(0)
  • 2020-12-10 11:47

    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".

    0 讨论(0)
提交回复
热议问题