mongoose-unique-validator not working?

Posted on Updated on

Are you using the mongoose-unique-validator package and not getting any errors when you post duplicate objects?

CURRENT

const personSchema = new mongoose.Schema({name:String,number: String})
personSchema.plugin(uniqueValidator)

SOLUTION

You need to set a unique attribute on the key you want uniqueness with. By default mongo only adds uniqueness to the __id key

const personSchema = new mongoose.Schema({name: {type:String, unique: true}, number: String})

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s