Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

396
Vistas
How can I increase and decrease the quantity of items on MongoDB?

I can update the quantity of items on MongoDB but I can not update the new quantity with the previous quantity. like I have 5$ but I want to update like ( 5+8 = $13) & (5-2 = $3). when I click the increase button then the value will be changed.

// mongoDb data

{ _id : 626c4f18fffe01027aa8c1be, quantity : "1" }

const Update = () => {
  const { updateId } = useParams();
  const [carInfo] = useCarDetails(updateId);  // [carInfo] get data by using specific id
  
  const addQuantity = (event) => {  // eventhandler
    event.preventDefault();
    const quantity = event.target.number.value;
    const updateCar = { quantity };
    console.log(updateCar);

    fetch(`http://localhost:5000/Cars/${updateId}`, {
      method: "PUT",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(updateCar),
    })
      .then((response) => response.json())
      .then((data) => {
        console.log("Success:", data);
        alert("updateCar added successfully");
        event.target.reset();
      });
  };
}

// This is put method of Mongodb

app.put("/Cars/:id", async (req, res) => {
      const id = req.params.id;
      const newCars = req.body;
      const filter = { _id: ObjectId(id) };
      const options = { upsert: true };
      const updateCar = {
        $set: {
          quantity: newCars.quantity,
        },
      };
      const result = await carCollection.updateOne(filter, updateCar, options);
      res.send(result);
    });

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda