Hello atk3,
Welcome to the MongoDB forums.
Has the unique index been created?
Can you run the db.collection.getIndexes() and send a screenshot of the results? Overall Mongoose isn’t really much of a solution for index management.
Due to this you may want to consider using the MongoDB Shell to handle these things, but that said indexes only have to be built if they are fresh, or you ran db.dropDatabase().
Mongoose v8.2.3: FAQ has an explanation on the unique section:
Q . I declared a schema property as
uniquebut I can still save duplicates. What gives?A . Mongoose doesn’t handle
uniqueon its own:{ name: { type: String, unique: true } }is just a shorthand for creating a MongoDB unique index onname. For example, if MongoDB doesn’t already have a unique index onname, the below code will not error despite the fact thatuniqueis true.
Regards,
Brock_GL