Mongodb数组$ push和$ pull [英] Mongodb array $push and $pull

查看:285
本文介绍了Mongodb数组$ push和$ pull的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直希望从数组中获取一些值,同时尝试对其进行更新.

I was looking to pull some value from array and simultaneously 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);
    }
  });
};

我遇到类似MongoError: exception: Cannot update 'profile.experience' and 'profile.experience' at the same time

推荐答案

我找到了以下解释:

问题在于MongoDB不允许对 在相同的更新调用中具有相同的属性.这意味着这两个 操作必须在两个单独的原子操作中进行.

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.

您可以阅读这些帖子:

使用mongo同时拉动和addtoset

单个语句中有多个mongo更新运算符?

这篇关于Mongodb数组$ push和$ pull的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆