'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.
Solution 1:[1]
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.
Solution 2:[2]
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 ;)
Solution 3:[3]
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})
Solution 4:[4]
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
Solution 5:[5]
as per your error, you have to delete a document with same key value pair, you just keep one
Solution 6:[6]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kim K |
| Solution 2 | |
| Solution 3 | caffeinated.tech |
| Solution 4 | Ali Raza |
| Solution 5 | party vadza |
| Solution 6 | Ajay jangid |
