Mongodian $ push and $ pull array

I was looking to get some value from an array and at the same time trying to update it.

    userSchema.statics.experience = function (id,xper,delet,callback) {
    var update = {
      $pull:{
        'profile.experience' : delet
      },

        $push: {
          'profile.experience': xper
        }
  };
  this.findByIdAndUpdate(id,update,{ 'new': true},function(err,doc) {
    if (err) {
      callback(err);
    } else if(doc){
      callback(null,doc);
    }
  });
};

i got an error, for example MongoError: exception: Cannot update 'profile.experience' and 'profile.experience' at the same time

+4
source share
1 answer

I found this explanation:

The problem is that MongoDB does not allow multiple operations on the same property in the same update call. This means that two operations must be performed in two separate atomic operations.

And you can read this post:

Pull and add mongo at the same time

multiple mongo update statements in one statement?

+4
source

Source: https://habr.com/ru/post/1619446/


All Articles