Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for Existing Document in Mongo

I'm new to Mongo and am using Mongoose as an ORM.

For user registration, I'm using email as the unique. I want to check if the email is already in use, and if it's not, then do something (perhaps save, perhaps next registration step, etc).

Sorry for what seems like a simple questions, but I haven't found an answer for it.

Unless it's "upsert". Which means inserting the document, even if I'm not quite ready to...

like image 804
bear Avatar asked Jan 01 '12 17:01

bear


1 Answers

Are you asking how to find a document?

db.users.findOne({email => '[email protected]'})

If it returns something, document exists. Otherwise, it doesn't.

like image 193
Sergio Tulentsev Avatar answered Oct 09 '22 08:10

Sergio Tulentsev