这是我的文件 请让我知道如何更新 cart.sizeQuantity.数量值 它应该更新productId匹配的值,然后检查尺寸匹配,然后更新
{
"name": "user",
"email": "[email protected]",
"password": "***************",
"cart": [
{
"productId": "1253456912854",
"sizeQuantity": [
{
"size": "Large",
"quantity": 2
}
]
},
{
"productId": "1253456912855",
"sizeQuantity": [
{
"size": "Large",
"quantity": 2
},
{
"size": "X-Large",
"quantity": 2
}
]
}
],
"__v": 0
}
我尝试过这样做,但这没有用
const hasSize = await this.findOne({
_id: userId,
"cart.productId": productId,
"cart.sizeQuantity.size": productSize,
});
if (hasSize) {
await this.updateOne(
{
_id: userId,
cart: { $elemMatch: { productId: productId } },
"cart.sizeQuantity.size": productSize,
},
{
$set: {
"cart.$.sizeQuantity.quantity": 4,
},
}
);
}