Nimit Bedi Nimit Bedi - 7 days ago 4x
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?


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.