Home > Enterprise >  How can I solve a "MongoError: E11000 duplicate key error collection" error in Mongo?
How can I solve a "MongoError: E11000 duplicate key error collection" error in Mongo?

Time:01-17

I have created a new Mongo collection to a new project, but when I try to create a new user, I get the following error:

MongoError: E11000 duplicate key error collection: GestorMedico.users index: username_1 dup key: { username: null }

My User schema is the following:

const { Schema, model } = require("mongoose");

const userSchema = new Schema({
    nombreEmpresa: {
        type: String,
        unique: true,
        required: true
    },
    email: {
        type: String,
        required: true
    },
    telefono: {
        type: String,
        required: true
    },
    contraseña: {
        type: String,
        required: true
    }
}, {
    timestamps: true,
    versionKey: false
});

module.exports = model("Users", userSchema);

My function is the following:

userCtrl.createUser = async (req, res) => {
    const newUser = new User(req.body);
    
    newUser.contraseña = await crypt.encryptPassword(newUser.contraseña);
    
    await newUser.save();
    
    res.status(200).json({
        status: "OK",
        message: "User created"
    });
};

And my collection looks like:

enter image description here

I have reused the backend of one of my old projects, which have a "username" in a schema.

CodePudding user response:

The Error E11000 is thrown when there are unique fields (indexes) that you try to save while there is another one already in the Database.

since the field seems to be username which is not visible in the userSchema, I am assuming you still have that index existing in your Database.

In mongo Compass on the page u made a screenshot from, go to the tab "Indexes", refresh and delete the username index in case it is there.

If that does not solve it proceed to print out the indexes via code and check there, if there is one that your don't want use db.collection.dropIndex() to delete it. Docs here

Also make sure that in your req.body the nombreEmpresa field is transferred since it is required when you want to save the document.

I hope that solves it ;)

CodePudding user response:

MongoError: E11000 duplicate key error collection: GestorMedico.users index: username_1 dup key: { username: null }

The error says that you have a username already that is set to null as you are not assigning values to the User object.

You are not assigning username and it seems undefined. When you are passing req.body to User, it won't assign keys like this. You need to make these changes:

userCtrl.createUser = async (req, res) => {
    const newUser = new User();
    newUser.email = req.body;
    newUser.nombreEmpresa = req.body;
    newUser.telefono = req.body;
    
    newUser.contraseña = await crypt.encryptPassword(newUser.contraseña);
    
    await newUser.save();
    
    res.status(200).json({
        status: "OK",
        message: "User created"
    });
};

Note: when you make fields required in the schema, you need to pass them.

You should not create an index on username as two or more users can have the same username. As it seems, you have a different email and username field. I would suggest making username(or keep it) as email(they are already unique) and make index on emails as they are unique.

  •  Tags:  
  • Related