I am trying to update multiple documents in a collection for an ecommerce workshop for school and I don't know how to update the documents that needs their quantity changed according to how much the client has bought
this is how the json body is being received from frontend:
{
"clientId": "123123",
"cartItems": [
{
"itemId":"6547",
"quantity":"3"
}, {
"itemId": "6543",
"quantity": "2"
}
],
"address": "123 main street",
"creditCardNumber":"1234567890123456"
}
I would like to iterate through the cartItems
array and update the my Items
collection in my database
Each document in my collection looks something like this:
{
"_id":"6543",
"name":"Barska GB12166 Fitness Watch with Heart Rate Monitor",
"price":"$49.99",
"body_location":"Wrist",
"category":"Fitness",
"imageSrc":"data:image/jpeg;base64...",
"numInStock":"9",
"companyId":"19962"
}
order.cartItems.forEach(async (item) => {
const result = await db
.collection("items")
.updateOne(
{ _id: parseInt(item.itemId) },
{ $inc: { numInStock: -parseInt(item.quantity) } }
);
});
then to restore the numInStock
result.cartItems.forEach(async (item) => {
const result = await db
.collection("items")
.updateOne(
{ _id: parseInt(item.itemId) },
{ $inc: { numInStock: parseInt(item.quantity) } }
);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.