Following is my user
schema in user.js
model -
var userSchema = new mongoose.Schema({
local: {
name: { type: String },
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
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.
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
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
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.
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).