E11000 duplicate key error index in mongodb mongoose

前端 未结 25 1565
自闭症患者
自闭症患者 2020-11-22 05:49

Following is my user schema in user.js model -

var userSchema = new mongoose.Schema({
    local: {
        name: { type: String },
         


        
相关标签:
25条回答
  • 2020-11-22 06:22

    I want to explain the answer/solution to this like I am explaining to a 5-year-old , so everyone can understand .

    I have an app.I want people to register with their email,password and phone number . In my MongoDB database , I want to identify people uniquely based on both their phone numbers and email - so this means that both the phone number and the email must be unique for every person.

    However , there is a problem : I have realized that everyone has a phonenumber but not everyone has an email address .

    Those that don`t have an email address have promised me that they will have an email address by next week. But I want them registered anyway - so I tell them to proceed registering their phonenumbers as they leave the email-input-field empty .

    They do so .

    My database NEEDS an unique email address field - but I have a lot of people with 'null' as their email address . So I go to my code and tell my database schema to allow empty/null email address fields which I will later fill in with email unique addresses when the people who promised to add their emails to their profiles next week .

    So its now a win-win for everyone (but you ;-] ): the people register, I am happy to have their data ...and my database is happy because it is being used nicely ...but what about you ? I am yet to give you the code that made the schema .

    Here is the code : NOTE : The sparse property in email , is what tells my database to allow null values which will later be filled with unique values .

    var userSchema = new mongoose.Schema({
      local: {
        name: { type: String },
        email : { type: String, require: true, index:true, unique:true,sparse:true},
        password: { type: String, require:true },
      },
      facebook: {
        id           : { type: String },
        token        : { type: String },
        email        : { type: String },
        name         : { type: String }
      }
    });
    
    var User = mongoose.model('User',userSchema);
    
    module.exports = User;

    I hope I have explained it nicely . Happy NodeJS coding / hacking!

    0 讨论(0)
  • 2020-11-22 06:24

    same issue after removing properties from a schema after first building some indexes on saving. removing property from schema leads to an null value for a non existing property, that still had an index. dropping index or starting with a new collection from scratch helps here.

    note: the error message will lead you in that case. it has a path, that does not exist anymore. im my case the old path was ...$uuid_1 (this is an index!), but the new one is ....*priv.uuid_1

    0 讨论(0)
  • 2020-11-22 06:24

    Here's how I solved same issue in September 2020. There is a super-fast and easy way from the mongodb atlas (cloud and desktop). Probably it was not that easy before? That is why I feel like I should write this answer in 2020.

    First of all, I read above some suggestions of changing the field "unique" on the mongoose schema. If you came up with this error I assume you already changed your schema, but despite of that you got a 500 as your response, and notice this: specifying duplicated KEY!. If the problem was caused by schema configuration and assuming you have configurated a decent middleware to log mongo errors the response would be a 400.

    Why is that? In my case was simple, that field on the schema it used to accept only unique values but I just changed it to accept repeated values. Mongodb on the past created an index for that field, and so even after setting "unique" property as "false" on schema, mongodb was still using that index.

    Solution? Dropping that index. You can do it in 2 seconds from Mongo Atlas.

    Go to your collection. By default you are on "Find" tab. Just select the next one on the right: "Indexes". You will see how there is still an index given to the same field is causing you trouble. Just click the button "Drop Index".

    I believe this is a better option than just dropping your entire collection. Basically because this is why it works after dropping the entire collection. Because mongo is not going to set an index if your first entry is using your new schema with "unique: false".

    0 讨论(0)
  • 2020-11-22 06:26

    This is because there is already a collection with the same name with configuration..Just remove the collection from your mongodb through mongo shell and try again.

    db.collectionName.remove()

    now run your application it should work

    0 讨论(0)
  • 2020-11-22 06:27

    The error message is saying that there's already a record with null as the email. In other words, you already have a user without an email address.

    The relevant documentation for this:

    If a document does not have a value for the indexed field in a unique index, the index will store a null value for this document. Because of the unique constraint, MongoDB will only permit one document that lacks the indexed field. If there is more than one document without a value for the indexed field or is missing the indexed field, the index build will fail with a duplicate key error.

    You can combine the unique constraint with the sparse index to filter these null values from the unique index and avoid the error.

    unique indexes

    Sparse indexes only contain entries for documents that have the indexed field, even if the index field contains a null value.

    In other words, a sparse index is ok with multiple documents all having null values.

    sparse indexes


    From comments:

    Your error says that the key is named mydb.users.$email_1 which makes me suspect that you have an index on both users.email and users.local.email (The former being old and unused at the moment). Removing a field from a Mongoose model doesn't affect the database. Check with mydb.users.getIndexes() if this is the case and manually remove the unwanted index with mydb.users.dropIndex(<name>).

    0 讨论(0)
  • 2020-11-22 06:28

    Check collection indexes.

    I had that issue due to outdated indexes in collection for fields, which should be stored by different new path.

    Mongoose adds index, when you specify field as unique.

    0 讨论(0)
提交回复
热议问题