There's no syntax with &
sign. MongoDB offers $
as a positional operator which allows you to modify existing item but you don't needed since you just want to append a new object to an array, try:
{
$addToSet: {
"cart.items": {
product_id: req.query.product_id,
quantity: 1,
}
}
}
Cart.updateMany(
{ user_id: decoded._id },
{
$push: {
"cart.items": {
product_id: req.query.product_id,
quantity: 1,
},
},
}
)
I finally found the right method through trial and error.
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.