MongoDB dup key: { : null }

Viewed 2971

I don't have a lot of MongoDB experience, and I'm running into the following with an mLab instance I have:

E11000 duplicate key error index: development.users.$key_1 dup key: { : null } MongoError: E11000 duplicate key error index: development.users.$key_1 dup key: { : null }

I've tried sorting by the key field, looking for a null key, and am not sure how I should go about finding the duplicate null key. What makes this difficult is that I'm not even sure what to search Google or stack overflow for to work through this. Any help would be appreciated, and below is the full error:

Mongoose model 'index-single-done' event fired on 'Family' for index:
Object {key: 1}

With error:
    E11000 duplicate key error index: development.users.$key_1 dup key: { : null } MongoError: E11000 duplicate key error index: 
development.users.$key_1 dup key: { : null }
at Function.MongoError.create (/Users/myUserName/development/projectName/node_modules/mongodb-core/lib/error.js:31:11)
at /Users/myUserName/development/projectName/node_modules/mongodb-core/lib/connection/pool.js:497:72
at authenticateStragglers (/Users/myUserName/development/projectName/node_modules/mongodb-core/lib/connection/pool.js:443:16)
at Connection.messageHandler (/Users/myUserName/development/projectName/node_modules/mongodb-core/lib/connection/pool.js:477:5)
at Socket.<anonymous> (/Users/myUserName/development/projectName/node_modules/mongodb-core/lib/connection/connection.js:333:22)
at Socket.emit (events.js:180:13)
at Socket.emit (domain.js:422:20)
at addChunk (_stream_readable.js:274:12)
at readableAddChunk (_stream_readable.js:261:11)
at Socket.Readable.push (_stream_readable.js:218:10)
at TCP.onread (net.js:581:20)
Mongoose model 'index' event fired on 'Family' with error:
E11000 duplicate key error index: development.users.$key_1 dup key: { : null } MongoError: E11000 duplicate key error index: development.users.$key_1 dup key: { : null }
at Function.MongoError.create (/Users/myUserName/development/projectName/node_modules/mongodb-core/lib/error.js:31:11)
at /Users/myUserName/development/projectName/node_modules/mongodb-core/lib/connection/pool.js:497:72
at authenticateStragglers (/Users/myUserName/development/projectName/node_modules/mongodb-core/lib/connection/pool.js:443:16)
at Connection.messageHandler (/Users/myUserName/development/projectName/node_modules/mongodb-core/lib/connection/pool.js:477:5)
at Socket.<anonymous> (/Users/myUserName/development/projectName/node_modules/mongodb-core/lib/connection/connection.js:333:22)
at Socket.emit (events.js:180:13)
at Socket.emit (domain.js:422:20)
at addChunk (_stream_readable.js:274:12)
at readableAddChunk (_stream_readable.js:261:11)
at Socket.Readable.push (_stream_readable.js:218:10)
at TCP.onread (net.js:581:20)
2 Answers

I encountered with the same issue

I have tried different techniques either looking the database validations or the database values but nothing found invalid that could produce this error.

finally deleted the collections and problem solved. Though it is not the good solution but in testing phase it is OK.

duplicate key error index: development.users.$key_1 dup key: { : null }

This means that your key field is set to accept only unique values and there is already one record with key having null value and you cannot insert another document with key as null. It is similar to having primary key in SQL databases.

Check your mongoose schema as it looks like you are using mongoose.

Most likely:

  1. while saving a record you're not passing key
  2. If you're passing key, it's value is null
Related