E11000 duplicate key error index in mongodb mongoose

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

    In this situation, log in to Mongo find the index that you are not using anymore (in OP's case 'email'). Then select Drop Index

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

    Drop you database, then it will work.

    You can perform the following steps to drop your database

    step 1 : Go to mongodb installation directory, default dir is "C:\Program Files\MongoDB\Server\4.2\bin"

    step 2 : Start mongod.exe directly or using command prompt and minimize it.

    step 3 : Start mongo.exe directly or using command prompt and run the following command

    i) use yourDatabaseName (use show databases if you don't remember database name)

    ii) db.dropDatabase()

    This will remove your database. Now you can insert your data, it won't show error, it will automatically add database and collection.

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

    I was facing similar issue, I went about clearing idexes and restarting server and probably almost everything mentioned here according to my understanding and still there was error, turns out the jsonwebtoken secret key that i was passing was undefined, if you are using jsonwebtoken, do check it once

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

    If you are still in your development environment, I would drop the entire db and start over with your new schema.

    From the command line

    ➜ mongo
    use dbName;
    db.dropDatabase();
    exit
    
    0 讨论(0)
  • 2020-11-22 06:37

    I have also faced this issue and I solved it. This error shows that email is already present here. So you just need to remove this line from your Model for email attribute.

    unique: true
    

    This might be possible that even if it won't work. So just need to delete the collection from your MongoDB and restart your server.

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

    Check indexes of that collection in MongoDB compass and remove those indexes which are not related to it or for try remove all indexes(Not from code but in db).

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