MongoDB Error Duplicate key

I sometimes get this error when authenticating with MongoDB:
errorResponse: {
apps/backend dev: index: 0,
apps/backend dev: code: 11000,
apps/backend dev: errmsg: 'E11000 duplicate key error collection: pushwave.sessions index: id_1 dup key: { id: null }',
apps/backend dev: keyPattern: { id: 1 },
apps/backend dev: keyValue: { id: null }
apps/backend dev: },
errorResponse: {
apps/backend dev: index: 0,
apps/backend dev: code: 11000,
apps/backend dev: errmsg: 'E11000 duplicate key error collection: pushwave.sessions index: id_1 dup key: { id: null }',
apps/backend dev: keyPattern: { id: 1 },
apps/backend dev: keyValue: { id: null }
apps/backend dev: },
It is because my id is saved under column _id not id therefore all sessions have id null - duplicate. How can I change which column is checked for ID? Because until now the only possible fix was to delete all sessions from table.
7 Replies
bekacru
bekacru4w ago
are you using the mongodb adapter or prisma?
jan
janOP4w ago
MongoDB adapter
bekacru
bekacru4w ago
There should be no id field. The monogodb adapter forces the _id field.
jan
janOP4w ago
yes but for some reason better auth checks for id not _id in some cases
bekacru
bekacru4w ago
If you can track what endpoint or action is exactly triggering that, let me know. Will take a look.
jan
janOP4w ago
hmm is it possible because of this?
No description
jan
janOP4w ago
when i authenticate with google it tries to create a new session in db with some _id and id set as null, but there is already a session in db existing with id null

Did you find this page helpful?