Nimit Bedi Nimit Bedi - 10 months ago 58
Node.js Question

Duplicate Error on adding data in mongoDB

I am trying to insert data in mongoDB document but I am getting duplicate error in it.


While defining the schema, I have given the value of "role" as default:"customer".
But on adding data with different number, i am getting this error.

Following is the schema defined:

Where is the error?

Answer Source

Check if the collection have unique index on the column.


here you will get unique index on userName. drop it

It will return all the indexes. If it have unique: true then drop it and then insert records.

OR just use


It's not the error because of mobilenumber. it's because of username. you are trying to insert multiple username with null value which is violating unique rule.