Mongoose findOne with 'either or' query

CaribouCode

I have a Mongo user database that I'm querying with Mongoose. I want to do findOne to determine if a user already exists. I want it to first search if a user already exists with an email, and if it doesn't it should then search to see if a user exists with a phone. Does this have to be done in 2 separate queries or can it be rolled into one?

User.findOne({ email: req.body.email }).exec(function(err, user){

  if (user) //user already exists with email
  else //no users with that email but we haven't checked phone number yet!

});
Andrew Dunai

Why not just use $or operator?

User.findOne({$or: [
    {email: req.body.email},
    {phone: req.body.phone}
]}).exec(function(err, user){
    if (user) {} //user already exists with email AND/OR phone.
    else {} //no users with that email NOR phone exist.
});

Here's the pseudo-SQL equivalent:

SELECT * FROM users WHERE email = '%1' OR phone = '%2'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mongoose Model.FindOne and Model.Find with one query

From Dev

Understanding mongoose findOne().remove()

From Dev

Mongoose findOne not completing

From Dev

Understanding mongoose findOne().remove()

From Dev

Mongoose findOne not returning anything

From Dev

Mongoose - FindOne() inside Multer

From Dev

Does the shape of json object have to match when you make a mongoose query with findOne?

From Dev

Mongoose searching FindOne with multiple arguments

From Dev

Mongoose findOne() vs limit(1)

From Dev

Mongoose findOne embedded document by _id

From Dev

Mongoose findOne embedded document by _id

From Dev

findOne isn't working with mongoose

From Dev

mongoose Model.findOne TypeError: Object has no method 'findOne'

From Dev

Using findOne then save() to replace a document, mongoose

From Dev

Mongoose find result and then replace field with findOne

From Dev

Does mongoose findOne on model return a promise?

From Dev

Mongoose JS findOne always returns null

From Dev

Mongoose findOne array of ObjectId returns null

From Dev

mongoose findOne() call not saving and no error in output

From Dev

What does findOne() return exactly in mongoose

From Dev

Function using Mongoose findOne returning undefined?

From Dev

mongoose findOne does not give the document even if it is there

From Dev

mongoose findOne with multiple paramters does not work

From Dev

Mongoose findOne function returns null/undefined

From Dev

Mongo DB issue with findOne query

From Dev

Mongoose or query

From Dev

Invalid `path`. Must be either string or array mongoose

From Dev

Mean: Query related collections gives 'findOne' of undefined

From Dev

node, mongoose 'findOne' on one collection inside 'find' of another collection

Related Related

  1. 1

    Mongoose Model.FindOne and Model.Find with one query

  2. 2

    Understanding mongoose findOne().remove()

  3. 3

    Mongoose findOne not completing

  4. 4

    Understanding mongoose findOne().remove()

  5. 5

    Mongoose findOne not returning anything

  6. 6

    Mongoose - FindOne() inside Multer

  7. 7

    Does the shape of json object have to match when you make a mongoose query with findOne?

  8. 8

    Mongoose searching FindOne with multiple arguments

  9. 9

    Mongoose findOne() vs limit(1)

  10. 10

    Mongoose findOne embedded document by _id

  11. 11

    Mongoose findOne embedded document by _id

  12. 12

    findOne isn't working with mongoose

  13. 13

    mongoose Model.findOne TypeError: Object has no method 'findOne'

  14. 14

    Using findOne then save() to replace a document, mongoose

  15. 15

    Mongoose find result and then replace field with findOne

  16. 16

    Does mongoose findOne on model return a promise?

  17. 17

    Mongoose JS findOne always returns null

  18. 18

    Mongoose findOne array of ObjectId returns null

  19. 19

    mongoose findOne() call not saving and no error in output

  20. 20

    What does findOne() return exactly in mongoose

  21. 21

    Function using Mongoose findOne returning undefined?

  22. 22

    mongoose findOne does not give the document even if it is there

  23. 23

    mongoose findOne with multiple paramters does not work

  24. 24

    Mongoose findOne function returns null/undefined

  25. 25

    Mongo DB issue with findOne query

  26. 26

    Mongoose or query

  27. 27

    Invalid `path`. Must be either string or array mongoose

  28. 28

    Mean: Query related collections gives 'findOne' of undefined

  29. 29

    node, mongoose 'findOne' on one collection inside 'find' of another collection

HotTag

Archive