I’m trying to update using the ID of the object inside the array but I can’t do it passing the objects in $set through a variable. This is my code that I can’t get to work:
const itemToUpdate = {
concept: req.body.concept,
incomeAmount: req.body.incomeAmount,
description: req.body.description,
expenseAmount: req.body.expenseAmount,
'createdBy':{
uid: req.uid,
username: 'poshed',
}
};
let pettyCashItem = await PettyCashItems.findOneAndUpdate({
_id: id,
"items._id": idItem },
{
$set: { itemToUpdate }
}, { new: 'true'});
But it happens that if I do it in the following way indicating object pot object it works but you have to write one by one:
let pettyCashItem = await PettyCashItems.findOneAndUpdate({
_id: id,
"items._id": idItem },
{
$set: { "items.$.concept": 'Test' }
}, { new: 'true'});
Thanks!