0

First, I'd like to start by saying I know there are several other posts out there somewhat related to inserting/updating if not exists; however, my case is a bit different and doesn't fit other posts.

I'm trying to insert a new user registration record if the user's email doesn't exist. All I'm doing right now is inserting the record.

One problem is that duplication checking with upsert:true doesn't really work because of all the unique values that get generated when a record is going to be inserted like the JSON Web Token, and Created Date, etc. Here is my current code:

MongoClient.connect('mongodb://localhost:27017/norad', function(err, db){
if (err) return router.error( Error(err) );
db.collection('users', function(err, coll){
    if (err) return router.error( Error(err) );

    coll.insert({
                 'email'    : useremail
                ,'pass'     : userpass
                ,'total_req': 0
                ,'threshold_req': 0
                ,'threshold_start_time': 0
                ,'ak'       : activation_key
                ,'activated': false
                ,'adm'      : false
                ,'iat'      : iat
                ,'created'  : iat
                ,'modified' : iat
            }
            ,function(err, writeResults){
                console.log( writeResults );
                if ( err && err.code == 11000 ) return router.error( ErrorHandler('User already exists.', 'Conflict', 409) )
                if ( err ) return router.error( Error(err) );
                var r = {'email': writeResults.ops[0].email
                        ,'Response':'Your activation code has been emailed to the address used in registration. Please check your email and follow the instructions for account activation.'
                        }

                    ,scallback = function(){ return router.submit(r) }
                    // send the activation email to the user
                    ,res = ActivationEmail(activation_key, useremail, scallback, router)
                    ;
                if ( res instanceof Error ) router.error( Error(err) );
            }
);

});
});

I'm not getting any error from this. Instead, this works fine and inserts a new record with every call, even if the user's email is already present in the table.

I'm trying to add functionality to check if the user's email is present in the table already, and throw an error if so, otherwise, create a new record.

3
  • Post the error as well. Commented Dec 4, 2017 at 8:42
  • Create a unique index on users.email. Commented Dec 4, 2017 at 9:25
  • @AlexBlex can you create an answer with this solution to show how this would look? Commented Dec 4, 2017 at 13:39

1 Answer 1

1

You need to create unique index on email field. E.g. in mongo shell:

db.users.createIndex(
   { email: 1},
   { unique: true } 
);
Sign up to request clarification or add additional context in comments.

1 Comment

Alex, I did what you recommended and the 11000 error code is now being thrown successfully upon duplicate email entry. This solved my problem. Thank you

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.