E11000 duplicate key error index in mongodb mongoose

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

    I had the same issue when i tried to modify the schema defined using mangoose. I think the issue is due to the reason that there are some underlying process done when creating a collection like describing the indices which are hidden from the user(at least in my case).So the best solution i found was to drop the entire collection and start again.

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

    I had same Issue. Problem was that I have removed one field from model. When I dropped db it fixes

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

    I faced similar issues , I Just clear the Indexes of particular fields then its works for me . https://docs.mongodb.com/v3.2/reference/method/db.collection.dropIndexes/

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

    I got this same issue when I had the following configuration in my config/models.js

    module.exports.models = {
      connection: 'mongodb',
      migrate: 'alter'
    }
    

    Changing migrate from 'alter' to 'safe' fixed it for me.

    module.exports.models = {
      connection: 'mongodb',
      migrate: 'safe'
    }
    
    0 讨论(0)
  • 2020-11-22 06:18

    Change the collection name if it already exists in the database, it will show an error. And if you given any property as unique the same error will occur.

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

    It's not a big issue but beginner level developers as like me, we things what kind of error is this and finally we weast huge time for solve it.

    Actually if you delete the db and create the db once again and after try to create the collection then it's will be work properly.

    ➜ mongo
    use dbName;
    db.dropDatabase();
    exit
    
    0 讨论(0)
提交回复
热议问题