So, I read all the answers to my problem on Stackoverflow and other websites and none of the solutions have helped me.
The following is the exact error –
MongoBulkWriteError: E11000 duplicate key error collection: test.users index: email_1 dup key: { email: "[email protected]" }
Below is my schema –
import mongoose from "mongoose";
const UserSchema = new mongoose.Schema(
{
firstName: {
type: String,
required: true,
min: 2,
max: 50,
},
lastName: {
type: String,
required: true,
min: 2,
max: 50,
},
email: {
type: String,
required: true,
max: 50,
unique: true,
},
password: {
type: String,
required: true,
min: 5,
},
picturePath: {
type: String,
default: "",
},
friends: {
type: Array,
default: [],
},
location: String,
occupation: String,
viewedProfile: Number,
impressions: Number,
},
{
timestamps: true,
}
);
const User = mongoose.model("User", UserSchema);
export default User;
I tried dropping the index email_1
that is causing the problem from the website (graphically).
However, that doesn’t seem to do anything. And email_1
is created every time I try to run my server.
I am reluctant on deleting my entire database because IF this were to happen to me in production, deleting the entire db would not be an option. I want to fix the issue.
Any help will be appreciated. Thank you.