![](/img/trans.png)
[英]Use findOneAndUpdate $pull and populate does not update or Populate
[英]how to use data to update something with findOneAndUpdate()
我希望更新我的數據庫中的某些內容,但我需要在數據庫中的對象中使用數據。
這是我想做的:
score.findOneAndUpdate({name: "global"}, {$set:{score: score+1}})
這是它在 db 中的樣子:
{
name: "global"
score: 1
}
所以我需要找到全局,並將分數加一並更新它以獲得
{
name: "global"
score: 2
}
我知道我可以先找到並存儲數據,然后再更新,但是我怎么能用 findAndUpdate() 做一次呢?
您可以改為使用$inc運算符來增加score
字段:
score.findOneAndUpdate({ name: "global" }, { $inc: { score: 1 } });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.