Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E11000 duplicate key error collection #289

Open
dinesh6383 opened this issue Nov 18, 2022 · 1 comment
Open

E11000 duplicate key error collection #289

dinesh6383 opened this issue Nov 18, 2022 · 1 comment

Comments

@dinesh6383
Copy link

I couldn't be able to register through facebook once after i registered and logout out from google auth. Kindly someone help me to solve this issue!.
/////////////////////////////////////////////////////////////////////////////////////////////////////////
MongoServerError: E11000 duplicate key error collection: usersDB.users index: username_1 dup key: { username: null }
at E:\backend-development\Secrets - Starting Code\node_modules\mongodb\lib\operations\insert.js:53:33
at E:\backend-development\Secrets - Starting Code\node_modules\mongodb\lib\cmap\connection_pool.js:308:25
at E:\backend-development\Secrets - Starting Code\node_modules\mongodb\lib\sdam\server.js:213:17
at handleOperationResult (E:\backend-development\Secrets - Starting Code\node_modules\mongodb\lib\sdam\server.js:329:20)
at Connection.onMessage (E:\backend-development\Secrets - Starting Code\node_modules\mongodb\lib\cmap\connection.js:219:9)
at MessageStream. (E:\backend-development\Secrets - Starting Code\node_modules\mongodb\lib\cmap\connection.js:60:60)
at MessageStream.emit (node:events:527:28)
at processIncomingData (E:\backend-development\Secrets - Starting Code\node_modules\mongodb\lib\cmap\message_stream.js:132:20)
at MessageStream._write (E:\backend-development\Secrets - Starting Code\node_modules\mongodb\lib\cmap\message_stream.js:33:9)
at writeOrBuffer (node:internal/streams/writable:389:12) {
index: 0,
code: 11000,
keyPattern: { username: 1 },
keyValue: { username: null },
[Symbol(errorLabels)]: Set(0) {}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////

@mdahtotul
Copy link

This is a monogo error. In your mongoose schema you maybe defined as username {type string, unique:true}. So when you are storing data in DB, only one null value for username will store. After that if username field has null value duplicate error will throw. Change username field as below

username: {
type: String,
index: true,
unique: true,
sparse: true,
}

After changing the schema. Drop that specific collection. And try again. No username field will only take multiple null values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants