Express+Mongoose - unique option not working

Hello support !

This subject has been discussed heavily in the past.
For some reason, unique does not enforce uniqueness on the property and I do not get a duplicate key error.
I am using create() function to create a new document.
I would like to avoid using find() for validation before issue a create() request.

I am using the latest version 6.0.5, M0 Sandbox (General) on AWS.
I’ve created a sample application to reproduce the issue: git@github.com:nirgluzman/Express-MongoDB-Recap.git

Thank you for your support !

Hello :wave: @Nir_Gluzman,

Welcome to the MongoDB Community forums :sparkles:

I tested your Git repository code base, and it worked perfectly fine for me. I received the following error in Postman when I tried to insert a document with the same email ID:

{ "error": "E11000 duplicate key error collection: test.blogs index: email_1 dup key: { email: \"mike@gmail.com\" }" }

To better understand the issue, could you share the workflow you followed to insert the document in MongoDB?

Could you please elaborate on what you meant by the above statement?

Best,
Kushagra

Hi Kushagra, thank you for helping.

Just a few hours ago, I updated the GitHub repo and added index: true to email attribute in the Schema.
This fix seems to solve the issue.

I found out that index:true enforces Mongoose to call createIndex for each defined index in the Schema when the collection is initially created - to be confirmed.
Without this option (having only unique: true), Mongoose does not issue createIndex.

https://mongoosejs.com/docs/faq.html#unique-doesnt-work
https://mongoosejs.com/docs/guide.html#indexes

I am not sure that this is the best solution.

Hello @Nir_Gluzman,

As mentioned in the Moongoose documentation, it is not an index management solution. Additionally, if you run this application against a populated database with duplicate data, it will not enforce a duplicate key constraint on the server-side.

I believe those are mongoose’s design decision. If you feel that this is not optimal for your use case, please open a Github issue on the mongoose repository.

Best,
Kushagra

Hi Kushagra,

Here are the steps to reproduce the issue.

Start Node.js application with npm start and establish connection to DB.
Drop database on MongoDB console
Send multiple POST with same email - no error received.

My question - what should I do on the Node.js side when I drop the database.

Many thanks
Nir

Hey @Nir_Gluzman,

Could you please clarify whether dropping the database is part of the application workflow? If it is, you need to ensure that the collection is recreated with all the necessary indexes and constraints.

Dropping the database or collection will also remove any indexes, including the one that enforces the unique constraint. Therefore, if you drop the database after starting the node application server, please restart it again. Otherwise, ensure that the database or collection does not already exist. I believe Mongoose will recreate it with the specified options (i.e. including the unique constraints).

Best,
Kushagra

Just Drop the collection.

Most likely you updated the schema for “unique” after creating some records. So “unique” is not working as per expectation. I fetched a similar issue and resolved it that way.

1 Like