0

For my users table, each user has an emails<Array> property allowing them to associate multiple emails with their accounts. I want to make sure this is still unique so wether they're creating a new account or updating existing I need to query the DB to identify if that email address exists.

I know I can use users.find({ emails: email }) and could loop over that to identify, then check the _id (on update) to ensure everything but that puts me in a loop outside the query.

I'm curious if there's a method I'm not seeing for querying to identify if any emails being submitted match any emails from across the table in the db?

2
  • Possible duplicate of MongoDB: query result match any value in array Commented Jun 7, 2019 at 19:01
  • 1
    Note: to alleviate any confusion about this being a duplicate, the $in operator works on array fields as well as non-array fields. To provide a specific answer, the query users.find({emails: {$in: [email]}}) should do the trick. Commented Jun 7, 2019 at 19:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.