MongoDB E11000 Duplicate "id" Key Error
I've been encountering this issue since I'm using Discord's OAUTH2 with BetterAuth while using MongoDB as an adapter. 
The exact error is as shown:
I've tried setting 
generateId to false like:
What could be the issue?Solution:Jump to solution
it could be that there are already fields in that webusers table with the 
id field & it's unique constraint, so even after you updated the mongoose schema it doesn't reflect mongo21 Replies
can I see your full auth config?
Thanks for the quick reply! Here's the full auth config, hope I didn't miss anything! I split it into two since I don't have nitro
when you use generateId: false, it's expected that your DB will generate the ID
Is that intentional for you?
I don't really understand, I had presumed that it uses the 
_id field which mongodb automatically generates. Could you explain what you mean?you shouldn't need to use generateId: false, better-auth handles id generation automatically
if you commented out your code for 
mapProfileToUser does everything work?I just tried it and commented out 
generateId and mapProfileToUser,
I used two accounts to test this:
Account 1 (email is available):
The account registers successfully, however id field is not set in the collection:
Account 2 (Email is empty)
It throws the error:
[Better Auth]: Provider did not return email. This could be due to misconfiguration in the provider settings.
A temporary workaround I did for this was using the mapProfileToUser methodid field is not set in the collection
_id for ids in mongo
A temporary workaround I did for this was using the mapProfileToUser methodI see so if you remove the generate id code entirely, and just keep the mapProfileToUser you then run into errors saying
id is null or something?I just ran the code like you said, removing the 
generateId field and restoring the mapProfileToUser method and it throws the original error:
did you index or change any of the schema for mongo?
I assume you mean the schema for the 
WebUser collection, I do have it set, am I supposed to?
Here's my WebUser schema:
we don't use 
id, we use _id like normal mongo tables
likely the reason for the errorOkay, I'll try changing it
I assume I just change it in the schema from 
id to _id, right?yeah, try that
normally we don't have schemas for mongo, we don't officially support mongoose technically
so not sure how this will go
I've changed the schema to:
And it indeed throws the same error still:
MongoServerError: E11000 duplicate key error collection: maximclient.webusers index: id_1 dup key: { id: null }
I'm out of ideas, if it's as you say, I think the issue might really be because I'm using mongoose, so maybe if I switched to the official mongodb package, it may workare you passing normal mongo client to the better-auth adapter? or mongoose
Yes, I'm passing the client from 
mongodb
And accessing the database using mongoose when it comes to using it outside of better-authAh that should be fine then
Solution
it could be that there are already fields in that webusers table with the 
id field & it's unique constraint, so even after you updated the mongoose schema it doesn't reflect mongocould it be possible to just reset that table?
I'll give it a try
Omg. It works like a charm. Thank you! I appreciate it a lot, that really was the issue!
awesome, nice