I've come across a challenge with my document collection:
{
_id: 1,
name: 'xpto',
arr: [1, 2, 3]
},
{
_id: 2,
name: 'xyz'
}
My goal is to add number 4 to the "arr" array using $addToSet and update {multi: 1}, but I encountered an error stating "Cannot apply $addToSet modifier to non-array." This error occurs because the second document does not have the "arr" field.
Is there a way to update an array using $push and/or $addToSet in all documents even if the array is not present in every one?
I attempted this using find, checking for cursor.arr and creating the array if it doesn't exist.
Do you know of any alternative approaches?