Skip to content

TodayILearned - 2023-04-26

Posted on:April 26, 2023 at 03:12 AM

#TIL

Today’s #TIL was a rather painful one..

Does anyone love errors that you only end up solving after like 30 minutes? Or is it just me? (sarcasm)

After finishing my Le-moment app, I’ve been working on my third final project for Nomad Coder’s 10 Sprint Challenge. The final project is a clone project of Youtube. It’s been really fun doing a comprehensive review on Node.js and Express, as well as using MongoDB for user, session, and video document data.

Today, while refactoring my POST method for the join page as well as my users database, I continuously had this error.

Mongo-Error

MongoServerError: E11000 duplicate key error collection: wetube.users index: username_1 dup key: { username: null }

I was incredibly confused by this error because my User model schema didn’t currently have a username field. Another curious phenomenon was I had no problem creating ONE user to the database, but from the second one, this error kept happening.

After many different attempts at trying to solve it myself, I finally looked to StackOverflow, and most posts suggested the following: Method #1: Delete the MongoDb collection and attempt again. Method #2: Look at your collection - there will be an extra index named username_1. Delete that index and try creating the second user again.

I tried the second method, and miraculously, I was able to add more users to my database.

The Why

I think the most likely reason this error occurred was because previously, my User model schema did have a ‘username’ field that was required AND had to be unique. However, while refactoring and building my project, I decided that having a user’s email act as a username itself and removed the username field from the User Schema. But because I did have username earlier, the username_1 index still existed.

Because username was previously a unique field, there was a username_1 unique index which was trying to ensure that there were no duplicate values under username fields across all created user accounts.

I read MongoDb’s documentation on unique index, more specifically, the Unique Index and Missing Field section:

If a document does not have a value for the indexed field in a unique index, the index will store a null value for this document. Because of the unique constraint, MongoDB will only permit one document that lacks the indexed field. If there is more than one document without a value for the indexed field or is missing the indexed field, the index build will fail with a duplicate key error.

You can combine the unique constraint with the sparse index to filter these null values from the unique index and avoid the error.

Voila. Because I previously had username as a required and unique field, the username_1 index was keeping track of whether usernames were repeated or not (EVEN THOUGH on my current Join page, username field wasn’t even offered in the form). So when the very first user was created, that user had a username of null, which is fine because null is a unique value. However, upon creating my second user account, the second user also had a username value of null, and therefore the subsequent account creation was not successful.

Thankfully, after deleting the username_1 index, things are back to normal. Phew. Honestly, I was so relieved and tired after solving this issue that I just wanted to move on to the next step of the project, but I felt like I had to document this to prevent future similar errors from happening.

Onwards!