Change in mongoose Schema does not reflect

Viewed 387

In my NodeJS project, I have created a mongoose schema which I changed midway through the project. More specifically, I added the unique parameter to a field.

However, this change does not seem to reflect as I am still able to create multiple documents with the same value for the parameter which I set as unique

How to fix this issue

1 Answers

Mongoose enforces the unique constraint by creating an index in MongoDB with the unique option.

If you already have duplicates in the collection, that index creation will fail.

Note that you may have to call syncIndexes to update the database with the new index.

Related