MongoDB setOnInsert and push if already existent
You could implement it without $setOnInsert
operator.
db.test.update(
{
name : 'Peter'
},
{
$push : {
"visits.en" : 'today'
}
},
{ upsert : true }
)
If Peter exists, element 'today'
will be added to its visits.en
array. Else, will be created a document for Peter, with visits
object, that will be contain array en
with 'today'
element.
And I think, that error occured because of you using same property (visits
) in two operations ($setOnInsert
and $push
).