question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unique index validation error not throwing

See original GitHub issue

I’m using this schema with latest npm mongoose:

var schema = new Schema({

    _id: Schema.ObjectId,
    email: {type: String, required: true, unique: true}

});

If I try to save a email that is already in db, I expect to get a ValidationError like if a required field is omitted. However this is not the case, I get a MongoError: E11000 duplicate key error index.

Which is not a validation error (happens even if I remove the unique:true).

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:13

github_iconTop GitHub Comments

3reactions
blakehaswellcommented, Jul 9, 2013

For anyone interested, I’ve added a plugin which checks the DB prior to saving and throws a Mongoose validation error if any of your unique constraints will be violated: https://github.com/blakehaswell/mongoose-unique-validator

1reaction
lukejacksonncommented, Apr 18, 2015

I had this issue and it was due to already existing duplicate values in my index (as pointed out in aheckmann’s comment on Feb 1, 2013). I was reading from here http://mongoosejs.com/docs/api.html#schematype_SchemaType-required where it mentions the error, but fails to mention about already exiting duplicates preventing the uniques index from being created in the first place. Might be worth updating the doc to mention this too!

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - Mongoose unique validation error even though ...
You may need to confirm this first by checking the indexes currently in the database; you will most probably find the email unique...
Read more >
How to handle unique constraint violations
Catch uniqueness exceptions thrown by the database at the lowest level possible — in the UnitOfWork class · Convert them into Result ·...
Read more >
Customise Index errors | OutSystems
Is it possible to customise the server error message that shows up from a unique index when a user tries to enter a...
Read more >
Unique Indexes — MongoDB Manual
A unique index ensures that the indexed fields do not store duplicate values; i.e. enforces uniqueness for the indexed fields. By default, MongoDB...
Read more >
Difference between Unique Indexes and Unique Constraints ...
We cannot drop the unique Index created by the unique constraints. If we try to do so, it gives you the following error...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found