I have an array of emails to delete as
[‘tim@apple.com’, ‘gates@microsoft.com’,'elon@tesla.com]
my collection contains :
userid: objectID,
fullname : …
contacts : [ {email: gates@microsoft.com } ]
what I need to do is either a findAndUpdate or findModify with a $pull to delete all contacts with email that equals to array of emails for a given userid.
all the items in the array are surely in the contacts list.
so far I have
collection(“users”).findOneAndUpdate({_id : ObjectId(userId)}, {$pull : {contacts : {email : deleteArray}}},{multi:true});
now if I run the above,it will not throw an error because I cant pass in a array as the value for email. it does work if I pass in a single email. I tried using pullAll, but that does not work either. it says expected array but passed in a object.
What ways can I delete all elements in a array without looping over the findoneandupdate for each element in the array.
thanks.