gpt4 book ai didi

Mongodb 数组 $push 和 $pull

转载 作者:IT老高 更新时间:2023-10-28 13:09:29 30 4
gpt4 key购买 nike

我希望从数组中提取一些值并同时尝试更新它。

    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

最佳答案

我找到了这个解释:

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.

你可以阅读那些帖子:

Pull and addtoset at the same time with mongo

multiple mongo update operator in a single statement?

关于Mongodb 数组 $push 和 $pull,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34217874/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com