rahul nirfarake
rahul nirfarake

Reputation: 47

check unique email in mongodb with node js

Actually I am using mongodb with Express js framework and check the email exists with my custom code and now I want to check the email is existed in my collection or not with the package that can easy to use with the Express js framework.

If_exists(req,res,callback) {
    var mongoose = require('mongoose');
    var Auth = mongoose.model('Auth');
    Auth.findOne({email:req.body.email}, function (err1, user1) {
      if (user1) {
        var error = "email";
        var message = "Email already exists";
        callback(error,message);
        return;
      }
      else {
        var error = "";
        var message = "";
        callback(error,message);
        return;
      }
    });
  }

Upvotes: 0

Views: 1852

Answers (1)

krupali makadiya
krupali makadiya

Reputation: 596

if you want to add unique records in mongoose collection using attribute

unique : true

in your model use like this email:{type: String, unique: true},

if you add like this then when you add new email and that email is already in collection then it's give you error

Upvotes: 1

Related Questions