'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);
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
