[英]Mongo DB Update data
我想將以前的數量減少 1 我怎么能在 Node Js Mongo Db 中做到這一點
這是我的代碼:
app.put('/quantityUpdate',async(req,res)=>{ const id = req?.body?.id; const dec= req?.body?.dec; const filter = {_id:ObjectId(id)} // this option instructs the method to create a document if no documents match the filter const options = { upsert: true }; const updateDoc = { $set: { quantity: //I'm stuck in this place }, }; const result = await products.updateOne(filter, updateDoc, options); return res.send(result); })
而不是$set
使用$inc
。 它按指定值遞增字段。
要將值減 1,請將代碼更改為:
const updateDoc = { $inc: { quantity: -1 } }
要了解更多詳細信息,請查看文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.