in the following example I have a company named Colors Company that owns many shops
{
_id: ObjectId("78391283"),
name: "Colors company",
shops: [
{shopID: ObjectId("123456a1cb"), income: 0},
{shopID: ObjectId("2a1cb67890"), income: 0},
{shopID: ObjectId("2a1cb010111"), income: 0},
...
],
}
I need to : 1- Loop on the shops array, find the requested array using {parsedBody.shopID} 2- Get use of the amount stored in “shop.$.income” as a number 2- Increase the income using {parsedBody.amountToAdd}
This is the POST request parsedBody :
{
companyID: "78391283",
shopID: "123456a1cb",
amountToAdd: 200,
}
I’m using Next js 13.
Thanks for your help !
What i’ve tried :
const ObjectId = require("mongoose").Types.ObjectId;
Shops.findOneAndUpdate(
{ _id: parsedBody.shopID},
{
set: {
// <-- missing loop here
"shops.$.income": { parsedBody.amountToAdd + income }
}
}
)
I am expecting to loop through the shops array and update the income field.