I need to update a property which is an array, existing within another array in my MongoDB collection. Now, this works:
{
"_id": request.parameters.id
},
{
$set: { "services.2.history": historyArray }
},
{
new: true
}
... but I don't want to trust that the element I'm targeting will always be the in position 2
. I do know the value of a property in the target element, however:
{
_id: <value>,
category: 'evaluation', // I know the correct array element has this value
startDate: <date>
}
So, that said, is there a way I can pass that value in here in order to target the correct array element within services
using the $[]
operator and arrayFilters
?
I tried this but it's not working:
{
_id: request.parameters.id
},
{
$set: { "services.$[element].history": historyArray }
},
{ arrayFilters: [{ element: { "$services.category": targetService.category } }] },
{
new: true
}
No errors. Just doesn't seem to update the document as expected. Am I missing something here? What should the syntax look like here? I want it to update the array element for services where the "category" property has the value of "evaluation".