Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

354
Views
sotre data in mongodb via mongoose schema with not passing value of unique true key

i have a mongoose schema like this :-

var UserSchema = new Schema({
name: {type: String, required: true},
email: {type: String, required: true,unique: true},
mobNum: {type: String, required: true, unique: true},
username: {type: String, unique: true},
dob:{ type: Date},
isTempUser: {type: String, default: true},
mobNumVerified: {type: Boolean, default: false},
emailVerified: {type: Boolean, default: false},
registrationComplete: {type: Boolean, default: false}})

When i try to save data in db with this code:-

                    let newUser = User();
                    newUser.name = name;
                    newUser.email = email;
                    newUser.mobNum = mobNum;
                    newUser.save();

when i run server and hit my api for register new user only frist time data save successfully in mongodb when i try different value save in db i got and error

that's :-

{ MongoError: E11000 duplicate key error collection: okhlee-kdb-promoter.users index: username_1 dup key: { : null }
    at Function.MongoError.create (D:\Okhlee.com\KDB Promoter backend\node_modules\mongodb-core\lib\error.js:31:11)
    at toError (D:\Okhlee.com\KDB Promoter backend\node_modules\mongodb\lib\utils.js:139:22)
    at D:\Okhlee.com\KDB Promoter backend\node_modules\mongodb\lib\collection.js:659:23
    at handleCallback (D:\Okhlee.com\KDB Promoter backend\node_modules\mongodb\lib\utils.js:120:56)
    at D:\Okhlee.com\KDB Promoter backend\node_modules\mongodb\lib\bulk\unordered.js:465:9
    at handleCallback (D:\Okhlee.com\KDB Promoter backend\node_modules\mongodb\lib\utils.js:120:56)
    at resultHandler (D:\Okhlee.com\KDB Promoter backend\node_modules\mongodb\lib\bulk\unordered.js:413:5)
    at D:\Okhlee.com\KDB Promoter backend\node_modules\mongodb-core\lib\connection\pool.js:461:18
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickCallback (internal/process/next_tick.js:104:9)
  name: 'MongoError',
  message: 'E11000 duplicate key error collection: okhlee-kdb-promoter.users index: username_1 dup key: { : null }',
  driver: true,
  code: 11000,
  index: 0,
  errmsg: 'E11000 duplicate key error collection: okhlee-kdb-promoter.users index: username_1 dup key: { : null }',
  getOperation: [Function],
  toJSON: [Function],
  toString: [Function] }

As i know this error is related to duplicate value of username but i when save data in db i'm not store username on frist api i save username next time when user come on our webpage .

So please solve this error.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

In MongoDB a unique field can not be empty in multiple records. If your user will not provide username at first but you still need the field to be unique, then you can store a random string or mobile num or email as username in the first attempt. I think that will work for you.

If you wish to create a random string and assign it to username, you can try the following using crypto:

  let newUser = User();
  newUser.name = name;
  newUser.email = email;
  newUser.mobNum = mobNum;
  require('crypto').randomBytes(48, function(err, buffer) {
        if(err) { throw err }
        username = buffer.toString('hex');
        newUser  = username;
        newUser.save();
  });
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!