我的資料集
{
"_id" : 1,
"item_id" : "I001",
"comp_id" : "C001",
"qty" : 25,
"prate" : 30,
"srate" : 35,
"mrp" : 40
},
{
"_id" : 2,
"item_id" : "I001",
"comp_id" : "C002",
"qty" : 30,
"prate" : 32,
"srate" : 37,
"mrp" : 40
}
我應該如何使用 MongoDB 增加“數量”?有什么幫助嗎?
uj5u.com熱心網友回復:
it is a backend MongoDB database API for quantity updating
app.put("/item/:id", async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const item = await itemCollection.findOne(query);
const newQuantity =
parseInt(item.quantity) parseInt(req.body.quantity);
await itemCollection.updateOne(query, {
$set: { quantity: newQuantity "" },
});
res.send(item);
});
it is client side code==
const qtyUpdate = () => {
const url = `https://localhost5000/item/${productDetail._id}`;
fetch(url, {
method: "PUT",
headers: {
"Content-type": "application/json",
},
body: JSON.stringify({ quantity: inputQuantity }),
})
.then((res) => res.json())
.then((data) =>
setProductDetail({
...productDetail,
quantity: productDetail.quantity parseInt(inputQuantity),
})
);
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/470079.html
標籤:javascript 节点.js 反应 mongodb 表示
上一篇:使用userAdminAnyDatabase角色獲取mongo授權錯誤
下一篇:嵌入谷歌地圖-設定最大縮放級別
