Hello : )
I think this will work.
var delete_emails = [{"email" :tim@apple.com"}, {"email" : "gates@microsoft.com"},{"email" : "elon@tesla.com"}];
collection(“users”).findOneAndUpdate({_id : ObjectId(userId)}, {$pullAll : {contacts : delete_emails }},{multi:true});
I haven’t tested it see examples also in docs.
You can also use pipeline updates that are more powerful,but i dont think you need it here.You can use something like the bellow(add the filter,bellow is only the update)
For more complicated updates you can use $filter with a pipeline update.With filter you could do it as.
[ {
"$addFields" : {
"contacts" : {
"$filter" : {
"input" : "$contacts",
"as" : "contact",
"cond" : {
"$not" : [ {
"$in" : [ "$$contact.email", ["tim@apple.com", "gates@microsoft.com","elon@tesla.com"]]
} ]
}
}
}
}
} ]
We don’t have sets in mongodb we only have arrays,so we cant do the check in O(1)
Unless mongodb converts it to a set internally,i dont know if it does.