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