I have a document structure like so:
{
data: null,
subArray: [{
key: 'a',
subData: []
}, {
key: 'b',
subData: []
}]
}
I will receive an array containing subArray keys, like: `['a', 'b']
I want to iterate over the subArray to push a value to subData, so that when subArray is ['a','b'], my data will now be like:
{
data: null,
subArray: [{
key: 'a',
subData: ['dataToPush']
}, {
key: 'b',
subData: ['dataToPush']
}]
}
I have this query to do that:
update({_id:"...", "subArray.key":{"$in":['a','b']},
{'$addToSet':{'subArray.$.subData':'...'}}})
Thinking that $in will let me loop through my subArray.
But I realize this does not work because $in gets the elements that match the elements in the array, so I will get my data but not iterate over my subArray.
Is it possible to complete this with a specific selector/modifier combo?