我想从数组中提取一些值并同时尝试更新它.
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);
}
});
};
Run Code Online (Sandbox Code Playgroud)
我得到的错误就像 MongoError: exception: Cannot update 'profile.experience' and 'profile.experience' at the same time
Nik*_*yev 10
如果您需要将一个数组值替换为另一个,您可以使用arrayFilters进行更新。
(至少,存在于 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 }
)
Run Code Online (Sandbox Code Playgroud)
这将用“new_value”替换所有“old_value”数组元素。
| 归档时间: |
|
| 查看次数: |
9912 次 |
| 最近记录: |