E11000 duplicate key error index in mongodb mongoose

前端 未结 25 1567
自闭症患者
自闭症患者 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:28

    I had the same issue. Tried debugging different ways couldn't figure out. I tried dropping the collection and it worked fine after that. Although this is not a good solution if your collection has many documents. But if you are in the early state of development try dropping the collection.

    db.users.drop();
    
    0 讨论(0)
  • 2020-11-22 06:30

    Well basically this error is saying, that you had a unique index on a particular field for example: "email_address", so mongodb expects unique email address value for each document in the collection.

    So let's say, earlier in your schema the unique index was not defined, and then you signed up 2 users with the same email address or with no email address (null value).

    Later, you saw that there was a mistake. so you try to correct it by adding a unique index to the schema. But your collection already has duplicates, so the error message says that you can't insert a duplicate value again.

    You essentially have three options:

    1. Drop the collection

      db.users.drop();

    2. Find the document which has that value and delete it. Let's say the value was null, you can delete it using:

      db.users.remove({ email_address: null });

    3. Drop the Unique index:

      db.users.dropIndex(indexName)

    I Hope this helped :)

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

    This is my relavant experience:

    In 'User' schema, I set 'name' as unique key and then ran some execution, which I think had set up the database structure.

    Then I changed the unique key as 'username', and no longer passed 'name' value when I saved data to database. So the mongodb may automatically set the 'name' value of new record as null which is duplicate key. I tried the set 'name' key as not unique key {name: {unique: false, type: String}} in 'User' schema in order to override original setting. However, it did not work.

    At last, I made my own solution:

    Just set a random key value that will not likely be duplicate to 'name' key when you save your data record. Simply Math method '' + Math.random() + Math.random() makes a random string.

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

    for future developers, i recommend, delete the index in INDEX TAB using compass... this NOT DELETE ANY document in your collection Manage Indexes

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

    Please clear the collection or Delete the entire collection from MongoDB database and try again later.

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

    I had a similar problem and I realized that by default mongo only supports one schema per collection. Either store your new schema in a different collection or delete the existing documents with the incompatible schema within the your current collection. Or find a way to have more than one schema per collection.

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