Is it possible to perform a push of an object to an array or update an existing object if it exists in the array depending on the value of an object property in the array in one command through the Javascript SDK? For example, if I have a field of type array as follows:
contacts: [
{
'name'
:
'john'
,
'available'
:
true
},
{
'name'
:
'doe'
,
'available'
:
false
}
]
Now if I take a new object where name:'john' (regardless of the property 'available') then I want to update the existing object for 'john'. However, if I have an object which name:'frank' then I want to add a new object to the array.
I know I can use the operator $addToSet, but AFAIK that will insert another object into the array if for example name:'john' but available:'false'. I also saw the $elemMatch operator which works on queries. I guess I am looking for a combination of these two.
Any advice?
Thanks!