Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8798207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:01:50+00:00 2026-06-14T00:01:50+00:00

I have to following code: var UserSchema = mongoose.Schema({ email: { type: String, unique:

  • 0

I have to following code:

var UserSchema = mongoose.Schema({
  email: {
    type: String,
    unique: true
  },
  username: {
    type: String,
    unique: true
  }
});

var User = db.model('User', UserSchema);

var user = new User();

user.email = 'some@email.com'
user.username = 'peter'

user.save(function(err){
  if (err) return console.log(err)
  console.log('user saved')
})

On the first run it shows user saved which is totally fine. On the second run it gives me

{ [MongoError: E11000 duplicate key error index: node-login.users.$username_1  dup key: { : "peter" }]
  name: 'MongoError',
  err: 'E11000 duplicate key error index: node-login.users.$username_1  dup key: { : "peter" }',
  code: 11000,
  n: 0,
  lastOp: 0,
  connectionId: 1455732,
  ok: 1 }

What about the email? It is also a duplicate and should return an error. If only one error is thrown each time, why is it the username and not email? email is above username in the schema. How does mongoose decide about what duplicate to check/return first?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:01:51+00:00Added an answer on June 14, 2026 at 12:01 am

    It’s not Mongoose that’s doing this, it’s MongoDB. Which one of the two duplicate fields that’s flagged is likely undefined. I assume it’s simply throwing the error as soon as the first index it checks fails the uniqueness test.

    If you need more information than what’s provided in the error, you’ll need to query for all potential duplicate fields separately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: var bool:String = true; Without an if block or
I have the following code: var string = word1;word2;word3,word4,word5,word6.word7; function ends_with(string, character) { var
I have the following code var section = new CustomConfigurationSection(); section.SectionInformation.Type = System.Configuration.NameValueFileSectionHandler; section.SectionInformation.SetRawXml(sectionXml);
I have the following code - var options = new Ext.data.JsonStore({ model: 'options_model', data:
I have the following code: var items = new List<string> {1, 2, 3}; //
I have the following code: var thing = (from t in things where t.Type
I have the following code: var x = 100.007 x = String(parseFloat(x).toFixed(2)); return x
I have the following code var query = from lookup in dataset.Tables[MBDDX_LOOKUP].AsEnumerable() where lookup.Field<string>(lookup_value)
I have the following code: var stereo_form =new Ext.form.FormPanel({ id: stereoInfo_panel, autoDestroy:true, frame: true,
I have following code var res = from c in model.Data select new object[]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.