[GH-ISSUE #2463] MongoDB Adapter Error: E11000 duplicate key error collection: #9203

Closed
opened 2026-04-13 04:35:51 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @janjurdik on GitHub (Apr 27, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/2463

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Hello,

I have been facing this error for almost a month now and couldn't find any solution to it, therefore I am asking if the issue might be in the library itself.

When I log in with my account, it creates a session in database. Then I remove the session key from cookies, reload the page and try to log in again. After which I get this error:

2025-04-27T11:51:28.091Z ERROR [Better Auth]: MongoServerError MongoServerError: E11000 duplicate key error collection: myProject.sessions index: id_1 dup key: { id: null }

therefore I can't log in with that account anymore. I tried setting advanced.generateId to false and it didn't help either

Thanks for your time

Current vs. Expected behavior

Current: DB error
Expected: Create new session and remove the old one preferably

What version of Better Auth are you using?

1.1.21

Provide environment information

OS: Windows 11
MongoDB adapter

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth";
import { adapter } from "@/core/mongoose";
import { openAPI } from "better-auth/plugins";
import { v4 as uuidv4 } from "uuid";

export const auth = betterAuth({
  database: adapter,
  account: {
    modelName: "accounts",
    accountLinking: {
      enabled: true,
    },
  },
  user: {
    modelName: "users",

    },
  },
  session: {
    modelName: "sessions",
  },
  verification: {
    modelName: "verifications",
  },
  emailAndPassword: {
    enabled: true,
    requireEmailVerification: true,
  },
  socialProviders: {
    google: {
      clientId: process.env.GOOGLE_CLIENT_ID as string,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
    },
  },
  
  databaseHooks: {
    user: {
      create: {
        before: async (user) => {
          return {
            data: {
              ...user,
              permission: "default",
              disabled: process.env.NODE_ENV === "production" ? true : false,
            },
          };
        },
      },
    },
  },
  plugins: [openAPI()],
  advanced: {
    generateId: false,
    cookiePrefix: "customAuth",
  },
  trustedOrigins: [process.env.WEBPAGE_URL as string],
});

export type UserType = typeof auth.$Infer.Session.user;

Additional context

No response

Originally created by @janjurdik on GitHub (Apr 27, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/2463 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce Hello, I have been facing this error for almost a month now and couldn't find any solution to it, therefore I am asking if the issue might be in the library itself. When I log in with my account, it creates a session in database. Then I remove the session key from cookies, reload the page and try to log in again. After which I get this error: 2025-04-27T11:51:28.091Z ERROR [Better Auth]: MongoServerError MongoServerError: E11000 duplicate key error collection: myProject.sessions index: id_1 dup key: { id: null } therefore I can't log in with that account anymore. I tried setting advanced.generateId to false and it didn't help either Thanks for your time ### Current vs. Expected behavior Current: DB error Expected: Create new session and remove the old one preferably ### What version of Better Auth are you using? 1.1.21 ### Provide environment information ```bash OS: Windows 11 MongoDB adapter ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth"; import { adapter } from "@/core/mongoose"; import { openAPI } from "better-auth/plugins"; import { v4 as uuidv4 } from "uuid"; export const auth = betterAuth({ database: adapter, account: { modelName: "accounts", accountLinking: { enabled: true, }, }, user: { modelName: "users", }, }, session: { modelName: "sessions", }, verification: { modelName: "verifications", }, emailAndPassword: { enabled: true, requireEmailVerification: true, }, socialProviders: { google: { clientId: process.env.GOOGLE_CLIENT_ID as string, clientSecret: process.env.GOOGLE_CLIENT_SECRET as string, }, }, databaseHooks: { user: { create: { before: async (user) => { return { data: { ...user, permission: "default", disabled: process.env.NODE_ENV === "production" ? true : false, }, }; }, }, }, }, plugins: [openAPI()], advanced: { generateId: false, cookiePrefix: "customAuth", }, trustedOrigins: [process.env.WEBPAGE_URL as string], }); export type UserType = typeof auth.$Infer.Session.user; ``` ### Additional context _No response_
GiteaMirror added the locked label 2026-04-13 04:35:51 -05:00
Author
Owner

@RiskyRomero commented on GitHub (Oct 28, 2025):

I'm encountering the exact same issue as you and setting generateId to false did not help, have you solved this, and if so, how did you do it?

<!-- gh-comment-id:3454419631 --> @RiskyRomero commented on GitHub (Oct 28, 2025): I'm encountering the exact same issue as you and setting `generateId` to false did not help, have you solved this, and if so, how did you do it?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#9203