[英]Mongodb, incrementing value inside an array. (save() ? update() ?)
[英]MongoDB Not Incrementing in Update Query
我正在嘗試增加購物車 object 中訂單項的數量。 每個 Line Item 都有自己的 ObjectId,因為可以添加相同的 item,但處理訂單所需的屬性不同。 這意味着我需要查看正確的行,而不是查看庫存項目。
數據庫記錄
Query ID's
{
cartId: '6307c7ccb275c35cb6bb2b71',
lineId: '6307c7d1b275c35cb6bb2b76'
}
Returned Results from Query No Update
{
_id: new ObjectId("6307c7ccb275c35cb6bb2b71"),
owner: '611afa8b9069c9126cff3357',
discount: null,
total: 101.94,
items: [
{
itemId: '61cb88f25f499fd4563e99d9',
sku: 1004,
quantity: 1,
price: 16.99,
_id: new ObjectId("6307c7d1b275c35cb6bb2b73")
},
{
itemId: '61cb88d85f499fd4563e99d3',
sku: 1001,
quantity: 1,
price: 16.99,
_id: new ObjectId("6307c7d1b275c35cb6bb2b76")
},
{
itemId: '61cb89255f499fd4563e99e7',
sku: 2003,
quantity: 1,
price: 16.99,
_id: new ObjectId("6307c7d3b275c35cb6bb2b79")
},
{
itemId: '61cb88df5f499fd4563e99d5',
sku: 1002,
quantity: 1,
price: 16.99,
_id: new ObjectId("6307c7d3b275c35cb6bb2b7c")
},
{
itemId: '61cb89255f499fd4563e99e7',
sku: 2003,
quantity: 1,
price: 16.99,
_id: new ObjectId("6307c7deb275c35cb6bb2b7f")
},
{
itemId: '61cb88d85f499fd4563e99d3',
sku: 1001,
quantity: 1,
price: 16.99,
_id: new ObjectId("6307cbcc3e81ef0f1a739cf9")
}
],
createdAt: 2022-08-25T19:04:44.859Z,
updatedAt: 2022-08-25T19:26:51.714Z,
__v: 0
}
exports.inc = async (req, res, next) => {
console.log(req.body);
const { cartId, lineId } = req.body;
console.log(cartId);
console.log(lineId);
try {
const updateCart = await Cart.findByIdAndUpdate(
{ _id: cartId, "item._id": lineId },
{ $inc: { quantity: 1 } },
{ new: true, returnDocument: "after" }
).exec();
if (!updateCart) return next(new ErrorResponse("Unable to update the cart record", 404));
console.log(updateCart);
if (updateCart) {
await calculateTotal(updateCart._id, updateCart.items, updateCart.discount);
return sendRes(updateCart, 200, res);
} else {
return sendRes(updateCart, 201, res);
}
} catch (error) {
console.log(error);
next(error);
}
};
查詢正在根據UpdateAt
值執行和處理更新,但數量沒有更新。
如果您發現任何問題,請參閱讓我知道。
嘗試這個
exports.inc = async (req, res, next) => {
console.log(req.body);
const { cartId, lineId } = req.body;
console.log(cartId);
console.log(lineId);
try {
const updateCart = await Cart.findByIdAndUpdate(
{ _id: cartId, "item._id": new mongoose.Types.ObjectId(lineId) },
{ $inc: { quantity: 1 } },
{ new: true, returnDocument: "after" }
).exec();
if (!updateCart) return next(new ErrorResponse("Unable to update the cart record", 404));
console.log(updateCart);
if (updateCart) {
await calculateTotal(updateCart._id, updateCart.items, updateCart.discount);
return sendRes(updateCart, 200, res);
} else {
return sendRes(updateCart, 201, res);
}
} catch (error) {
console.log(error);
next(error);
}
};
以下是對查詢的更正。
const updateCart = await Cart.findByIdAndUpdate(
{ _id: cartId, "items.&[element]._id": lineId },
{ $inc: { "items.$[element].quantity": 1 } },
{ new: true, returnDocument: "after", arrayFilters: [{ "element._id": lineId }] }
).exec();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.