【发布时间】:2021-09-30 19:18:24
【问题描述】:
当 productId 与 itemid 匹配时,我想增加购物车的 products 数组中的数量字段。我也想获得更新的文档作为回报。
这是我的购物车模型
import mongoose from 'mongoose';
const cartSchema = new mongoose.Schema({
userId: {
type: String,
required: true,
},
products: [
{
productId: {
type: String,
},
quantity: {
type: Number,
default: 1
}
}
]
}, {timestamps: true});
const Cart = new mongoose.model('Cart', cartSchema);
export default Cart;
这就是我正在做的事情:
const updatedCart = await Cart.findOneAndUpdate({"products.productId": itemid}, {$inc: {quantity: 1}}, {new: true})
//console.log(updatedCart)
但是一个空数组返回并且数量没有增加。
【问题讨论】:
标签: mongodb