Duplicate Key Error trying to insert a new document through mongoose

Hi, Pardon me if this is not the right topic/subtopic to ask questions. I was trying to insert a new document through nodejs mongoose Model.save(). However, the first document works. When I try to insert another document. I get the following error. E11000 duplicate key error collection: mydb.mycollection index: xxxxxx.xxxxx_xx_1 dup key: { xxxxxx.xxxxx_xx: null]. I would appreciate if someone can help me out.

Hi,

This means that you are passing null value for a field that should be unique.

So, when you try to insert first document, it works because there are still no document with null value for that field. When you try to insert another document, first document already exists with null value for that field, so duplicated key error is thrown.