I am working with a data structure:
{
field: 1,
field: 3,
field: [
{ _id: xxx , subfield: 1 },
{ _id: xxx , subfield: 1 },
]
}
My task is to update a specific element within the array.
Currently, my method involves removing the old object and inserting a new one, but this causes a change in the order of the elements.
This is how I am currently implementing it:
const product = await ProductModel.findOne({ _id: productID });
const price = product.prices.find( (price: any) => price._id == id );
if(!price) {
throw {
type: 'ProductPriceError',
code: 404,
message: `Coundn't find price with provided ID: ${id}`,
success: false,
}
}
product.prices.pull({ _id: id })
product.prices.push(Object.assign(price, payload))
await product.save()
I am exploring if there might be a more atomic way to achieve this. The current approach doesn't appear to be secure.