I’m interested in storing multiple email addresses for an individual in a single mongo document e.g.:
{
first_name: "Johnny"
last_name: "Tester"
emails: ["johnnytester@gmail.com", "johnnytester@fb.com"]
}
Is it possible for me to ensure that when I attempt to push a value on to the emails array that it doesn’t already exist? If it does exist I want it to be added but if it doesn’t exist I don’t want it to be added. I’m trying to avoid making two queries (read then write) which would also make it unsafe for threaded environments. Thanks!
And I’ve since found the answer using $addToSet, found here: http://www.mongodb.org/display/DOCS/Updating#Updating-%24addToSetand%24each