I have and object array as follows:
project: = {
id: 111,
title: "Project TEST",
tabs: [
{
tabId: 123,
products: [
{
uniqId: 1,
price: 180
quantity:5
},
{
uniqId: 2,
price: 80
quantity:2
},
]
},
{
tabId: 50,
products: [
{
uniqId: 11,
price: 180
quantity:5
},
{
uniqId: 12,// How I Identify this product inside the tab
price: 80 // I want to update all this Product Object
quantity:2 // I want to update all this Product Object
},
]
}
]
}
Given a project ID = 111, a tabId = 50, and a product uniqId = 12, I want to update all the object Product. First I found the Project with findOne. Then I have to get the tab and the products. I have used arrayFilters of mongose docs. It doesn't update the project in database.
How I can update this nested object array in the document?
async updateTabProduct(projectId,bodyProductToUpdate){
try{
const { Project: projectSchema } = this.getSchemas();
const projec0t = await projectSchema.findOneAndUpdate(
{
"_id": projectId,
"tabs": {
"$elemMatch": {
tabId, "products.uniqId": body.uniqId
}
}
},
{ "$set": {
"tabs.$[outer].products.$[inner].notes:": body.notes,
"tabs.$[outer].products.$[inner].nom:": "asdfafd",
} },
{ "arrayFilters": [
{ "outer.tabId": body.tabId },
{ "inner.uniqId": body.uniqId }
] }, (err, result) => {
if (err) {
console.log('Error updating service: ' + err);
} else {
}
});
return projec0t;
}
catch(error) {
throw error;
}
}