I am building an application with node js and mongodb/mongoose,to make data manipulation easier, I used mongodb pollution,and without wanting to I ended up in a situation where I am not sure of the performance of the code.
For example, let's say I have 3 collections: -category -product -variation
when I display the product, the json structure is something like this.
{
prodName:'test 1',
qtyProd:3,
......
category:[
{
_id:xcxxcxcxc,
catName:'Test Cat',
prodQty:10
},
{
_id:abababababab,
catName:'Test Cat 2',
prodQty:12
}
]
.............
variationProd:[
{
_id:dasdasdas,
//variation data
},
{
_id:dasdasdas,
//variation data
},
]
}
In situations where I delete a product, I have to do the following operations to delete all variations, and to adapt the quantity to all categories.
This is how my code looks like:Method:Patch
// Update Category Qty
exports.updateAllCat = async(req,res,next)=>{
//Category / Product => name of collection
const idProd = req.body.prodId;
await Product.findById(idProd ,(err,resp)=>{
if(err) ....
resp.category.forEach(async(item)=>{
//reduce prod qty from category
let newQty = item.prodQty - resp.qtyProd;
// update category qty
await Category.findByIdAndUpdate(item._id,{
prodQty:newQty
},(e,cb)=>{
if(err) ....
next()
})
})
})
}
// Delete Variation
exports.removeAllVariation = async(req,res,next)=>{
//Variation/ Product => name of collection
const idProd = req.body.prodId;
await Product.findById(idProd ,(err,resp)=>{
if(err) ....
resp.variationProd.forEach(async(item)=>{
//reduce prod qty from category
// update category qty
await Variation.findByIdAndDelete(item._id,(e,cb)=>{
if(err) ....
next()
})
})
})
}
//return response to client side
exports.returnResponse = async(req,res)=>{
res.status(200).json({
status:"success"
message:"Product deleted susccesfully"
})
}
But I'm not sure if this method of using forEach is a good one,because it could give error if there are many variations or categories.
I thought of separating the functions, a function in which to bring all the ids and the new quantity and then send them to the next function using res.locals.categoryLocal <<[{id:1223, newCat:7}]>> and res.locals.variationIds <<[123,324]>>,but in the end I would still end up using forEach.
Is there another option, I don't know anything from mongoose deleteMany() or updateMany(), another approach, in general from what I found on the internet and on youtube I'm a bit confused.