my database is as below
"_id":"xxxxxx",
"owner": "[email protected]",
"products": [
{
"name": "name",
"pid": 41,
"qty": 3
},
{
"name": "name2",
"pid": 42,
"qty": 4
}],
How can I change the qty value in products with pid value 41 in the nextjs api ? api/cart
if(method === "PUT") {
try {
const updateResult = await DbScart.findOneAndUpdate(
???
)
res.status(201).json({ success: true, data: updtprod });
} catch(err) {}
}
CodePudding user response:
One option is to use update with $[<identifier>]
:
{products: {$elemMatch: {pid: 41}}},
{$set: {"products.$[item].qty": newValue}},
{arrayFilters: [{"item.pid": 41}]}
See how it works on the playground example