簡體   English   中英

如何訪問文檔字段的先前值並在貓鼬的單個查詢中更新它

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM