【发布时间】:2021-06-25 06:57:33
【问题描述】:
我是猫鼬 ODM 的新手。我正在尝试通过另一个字段的值来增加一个值。以下是我尝试过的:
const deletedCartItem = await Cart.findOneAndUpdate(
{ sessionId: req.session.sessionId, "productInfo.productId": product_id },
{
$inc: {
checkoutAmount: "$productInfo.totalAmount"//totalAmount is a nested field here.
},
$pull: {
productInfo: { productId: product_id },
},
},
{ new: true, useFindAndModify: false }
);
我的收藏架构如下所示:
const cartSchema = mongoose.Schema({
sessionId: {
type: String,
unique: true,
},
checkoutAmount: {
type: Number,
},
orderStatus: {
type: String,
default: "INCART"
},
productInfo: [
{
productId: {
type: String,
required: true,
unique: true,
},
quantity: {
type: Number,
required: true,
},
price: {
type: Number,
},
totalAmount: {
type: Number,
}
}
],
});
我在这里遗漏了什么吗? 提前致谢!
【问题讨论】:
-
您认为提供的答案中是否有某些内容无法解决您的问题?如果是这样,请对答案发表评论,以澄清究竟需要解决哪些尚未解决的问题。如果它确实回答了您提出的问题,请注意Accept your Answers您提出的问题