MongoDB + Mongoose: Unique: true not working properly [duplicate] MongoDB + Mongoose: Unique: true not working properly [duplicate] mongoose mongoose

MongoDB + Mongoose: Unique: true not working properly [duplicate]


Try deleting the database and running your program again, this is maybe because you add the constraint after the database creation and mongoose no recreate the index.

if you cannot delete you database try this in the mongo console

db.users.createIndex({username:1}, {unique:true})

see mongo unique index for more information


You can also reindex your database in case you don't want to delete it.

mongo <db-name> db.<collection-name>.reIndex()