Kim K
Kim K

Reputation: 351

MongoError: E11000 duplicate key error collection: users index: mobile_1 dup key: { mobile: null }

I created a user schema where I have a mobile field. The mobile field should be unique but still allow null values and should only compare uniqueness with strings. Here's my debug info:

The 'mobile' key inside my user schema

mobile: {
  type: String,
  index: {
    unique: true,
    partialFilterExpression: { mobile: { $type: 'string' } }
  },
}

mongoose debug log

Mongoose: users.createIndex({ mobile: 1 }, { unique: true, partialFilterExpression: { mobile: { '$type': 'string' } }, background: true})

Error

MongoError: E11000 duplicate key error collection: users index: mobile_1 dup key: { mobile: null }
    at Function.create (~/project/node_modules/mongodb/lib/core/error.js:44:12)
    at toError (~/project/node_modules/mongodb/lib/utils.js:150:22)
    at ~/project/node_modules/mongodb/lib/operations/common_functions.js:265:39
    at handler (~/project/node_modules/mongodb/lib/core/sdam/topology.js:971:24)
    at ~/project/node_modules/mongodb/lib/core/sdam/server.js:496:5
    at ~/project/node_modules/mongodb/lib/core/connection/pool.js:420:18
    at processTicksAndRejections (internal/process/task_queues.js:75:11) {
  driver: true,
  name: 'MongoError',
  index: 0,
  code: 11000,
  keyPattern: { mobile: 1 },
  keyValue: { mobile: null },
  errmsg: 'E11000 duplicate key error collection: users index: mobile_1 dup key: { mobile: null }',
  [Symbol(mongoErrorContextSymbol)]: {}
}

I tried a lot of options and different answers from stackoverflow but nothing seems to work so I'd like ask the proper way of doing this.

EDIT

The issue was that the code I had was correct, but I had to delete and re-create the table for it to work. Another solution would be to just remove the indexes.

Upvotes: 4

Views: 20043

Answers (12)

Ashim Devnath
Ashim Devnath

Reputation: 1

I was also getting the same error with it when i change the mongodbURL of .env file the error is solved just a basic problem of mongodb hop this solution will save you day don't worry your code is always right

Upvotes: 0

JISHNU T RAJ
JISHNU T RAJ

Reputation: 18

I had this issue i just dropped the collection and added the data again

Upvotes: 0

Viraj Shinde
Viraj Shinde

Reputation: 1

You can connect the database with studio, go to appropriate collection, and find the field where duplicate error is pointing to. At the bottom, deselect Unique and Select Sparse. In case of me I was getting the same error with username field.I just did the above steps and it worked.

Note this is not recommended for fields that need to be unique(In case of mobile no) but you can do the above for fields where data is repeated such as username, likes, favorites etc.

Sometimes when a field is created through a schema, it turns it unnecessarily into unique.If you dont want it, you can change with above steps. Studio 3T makes it easy.

Upvotes: 0

Akshay Raut
Akshay Raut

Reputation: 81

-Open MongoDB collection (in which you are facing the problem) -Go to index tab and delete all the indexes that were showing the errors -you are good to go.

Upvotes: 0

I faced the same issue. I delete the database and create a new one ! then re run your application . It will work

Upvotes: 0

stackCode
stackCode

Reputation: 1

Try it this way go to your mongoDB database and click on database then click on indexes then delete some or all of the indexes, ofcourse when you are logged in .

cherrs

Upvotes: 0

Ajay jangid
Ajay jangid

Reputation: 1003

from myside the simple solution is just delete(drop) your database. probably this might resolve the issue,such issue may happens because of redefining of that collection's Schema.

Upvotes: 0

party vadza
party vadza

Reputation: 1

as per your error, you have to delete a document with same key value pair, you just keep one

Upvotes: 0

Ali Raza
Ali Raza

Reputation: 1

Just match the database key and value if any key does not match with your code delete the entire database then again push the data

Upvotes: 0

Mahdieh Shavandi
Mahdieh Shavandi

Reputation: 8645

After hours of googling, I finally found the problem

The problem was I was saving to a collection that I had previously used for another application with different userSchema that had several unique parameters like: phoneNumber, username ,...

But in my current models, I had just email and password as userSchema and I was getting this mongoError code 11000 with keyPattern: { username: null }

I just changed my Database_Url in my .env file (where I store my super secret data that don't want to hardcode in the app):

mongodb://127.0.0.1:27017/myNewCollectionName

and restarted the node with running:

npm start

then when i tried to register a user, I didn't get that error and it solved completely.

I hope this save your day ;)

Upvotes: 4

Kim K
Kim K

Reputation: 351

The issue was that the code I had was correct, but I had to delete and re-create the table for it to work. Another solution would be to just remove the indexes.

Upvotes: 19

caffeinated.tech
caffeinated.tech

Reputation: 6548

I think you need a sparse index. Right now the unique index means you can only have one user with a null mobile value

From MongoDB docs on Sparse Indexes

Sparse indexes only contain entries for documents that have the indexed field, even if the index field contains a null value. The index skips over any document that is missing the indexed field. The index is “sparse” because it does not include all documents of a collection. By contrast, non-sparse indexes contain all documents in a collection, storing null values for those documents that do not contain the indexed field.

Here is an example of it for your code

users.createIndex({ mobile: 1 }, { 
  unique: true,  
  sparse: true, 
  partialFilterExpression: { mobile: { '$type': 'string' } }, 
  background: true})

Upvotes: 0

Related Questions