Can you specify a key for $addToSet in Mongo?
You can qualify your update
with a query object that prevents the update if the name
is already present in profile_set
. In the shell:
db.coll.update( {_id: id, 'profile_set.name': {$ne: 'nick'}}, {$push: {profile_set: {'name': 'nick', 'options': 2}}})
So this will only perform the $push
for a doc with a matching _id
and where there isn't a profile_set
element where name
is 'nick'
.
As of MongoDB 4.2 there is a way to do this using aggregation expressions in update.
For your example case, you would do this:
newSubDocs = [ {'name' : 'matt', 'options' : 0}, {'name' : 'nick', 'options' : 2} ];db.coll.update( { _id:1 },[ {$set: { profile_set: {$concatArrays: [ "$profile_set", {$filter: { input:newSubDocs, cond: {$not: {$in: [ "$$this.name", "$profile_set.name" ]}} }} ]}}}])