[英]How to update a field using its previous value in MongoDB/Mongoose
[英]how to access the previous value of a document field and update it in a single query in mongoose
我想更新我的文檔字段“ investorCount”,以及在同一貓鼬查詢中同一文檔中的另一個更新。
我的更新查詢是
Campaign.update({"campaign.campaign_Id":data.campaignId},{$push: {"campaign.investorsDetails":{investorId: id,investedAmount: Amtinv }},$set : {"campaign.investorCount" : this."campaign.investorCount" + 1 }},function(error,update){
if(error){
defered.reject(error);
}else{
console.log(update);
defered.resolve(update);
}
});
return defered.promise;
}
我的模式是-
var campaignSchema = new Schema ({
campaign:
{
campaign_Id : {type: Number ,index :{unique:true}},
beneficiaryAccount : {type : String , required:true},
createdBy : {type : String },
investorsDetails : [{
investorId:{type:Number} ,
investedAmount : {type:Number}
}],
investorCount : {type : Number , default : 0}
}});
我使用'this'運算符來訪問字段的當前值,但是它不起作用,請幫助我解決這個問題
Mongo為此提供了方便的$ inc運算符:
$inc : {"campaign.investorCount" : 1 }}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.