Home > Software engineering >  How can i update qty quantity in mongodb database?
How can i update qty quantity in mongodb database?

Time:05-08

My Collection of data

{
        "_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
}

How should I increment the "qty" using MongoDB? any help?

CodePudding user response:

    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),
        })
      );
  };
  • Related