[GH-ISSUE #8668] MongoDB Adapter: setting UUIDs as document _id sets as string, not MongoDB's native UUID type #19787

Closed
opened 2026-04-15 19:07:53 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @yourlocaltechboi on GitHub (Mar 18, 2026).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/8668

Originally assigned to: @bytaesu on GitHub.

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Set advanced.database.generateId: () => { return crypto.randomUUID(); }
  2. Create user in frontend (I use Entra OAuth)
  3. Observe the user's ID get set in the database as a string.

Current vs. Expected behavior

Currently, it looks like the MongoDB Adapter only saves _ids as either strings or ObjectIds, but MongoDB/BSON has a native UUID type. The way UUIDs get stored in this manner breaks indexing when using a shared DB with another app that uses mongooses UUID SchemaType for example, and additionally is less performant than storing the UUID as the native type (increased storage as a string vs. a 16 byte binary representation). Could this be implemented in a manner that allows advanced.database.generateId: "uuid"? Currently setting generateId to "uuid" just falls back to ObjectIds entirely.

What version of Better Auth are you using?

1.3.34

System info

{
  "system": {
    "platform": "linux",
    "arch": "x64",
    "version": "#1 SMP PREEMPT_DYNAMIC Wed, 04 Mar 2026 18:09:01 +0000",
    "release": "6.18.16-1-lts",
    "cpuCount": 8,
    "cpuModel": "11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz",
    "totalMemory": "15.34 GB",
    "freeMemory": "5.90 GB"
  },
  "node": {
    "version": "v24.13.0",
    "env": "development"
  },
  "packageManager": {
    "name": "npm",
    "version": "11.9.0"
  },
  "frameworks": [
    {
      "name": "next",
      "version": "^16.1.7"
    },
    {
      "name": "react",
      "version": "19.2.4"
    }
  ],
  "databases": [
    {
      "name": "mongodb",
      "version": "^6.20.0"
    }
  ],
  "betterAuth": {
    "version": "^1.3.34",
    "config": {
      "baseURL": "http://localhost:3002",
      "basePath": "/api/auth",
      "emailAndPassword": {
        "enabled": false
      },
      "socialProviders": {
        "microsoft": {
          "clientId": "[REDACTED]",
          "clientSecret": "[REDACTED]",
          "tenantId": "common",
          "authority": "https://login.microsoftonline.com",
          "prompt": "select_account",
          "redirectURI": "http://localhost:3002/excalibur/api/auth/callback/microsoft"
        }
      },
      "advanced": {
        "database": {}
      },
      "plugins": [
        {
          "name": "next-cookies",
          "config": {
            "id": "next-cookies",
            "hooks": {
              "after": [
                {}
              ]
            }
          }
        }
      ]
    }
  }
}

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

Backend, Types, Documentation

Auth config (if applicable)


Additional context

UUIDs are needed for cross-platform compatibility, reverting to ObjectIDs is not suitable in this use-case.

Originally created by @yourlocaltechboi on GitHub (Mar 18, 2026). Original GitHub issue: https://github.com/better-auth/better-auth/issues/8668 Originally assigned to: @bytaesu on GitHub. ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. Set `advanced.database.generateId: () => { return crypto.randomUUID(); }` 2. Create user in frontend (I use Entra OAuth) 3. Observe the user's ID get set in the database as a string. ### Current vs. Expected behavior Currently, it looks like the MongoDB Adapter only saves `_id`s as either strings or `ObjectId`s, but MongoDB/BSON has a native UUID type. The way UUIDs get stored in this manner breaks indexing when using a shared DB with another app that uses `mongoose`s UUID SchemaType for example, and additionally is less performant than storing the UUID as the native type (increased storage as a string vs. a 16 byte binary representation). Could this be implemented in a manner that allows `advanced.database.generateId: "uuid"`? Currently setting `generateId` to `"uuid"` just falls back to `ObjectId`s entirely. ### What version of Better Auth are you using? 1.3.34 ### System info ```bash { "system": { "platform": "linux", "arch": "x64", "version": "#1 SMP PREEMPT_DYNAMIC Wed, 04 Mar 2026 18:09:01 +0000", "release": "6.18.16-1-lts", "cpuCount": 8, "cpuModel": "11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz", "totalMemory": "15.34 GB", "freeMemory": "5.90 GB" }, "node": { "version": "v24.13.0", "env": "development" }, "packageManager": { "name": "npm", "version": "11.9.0" }, "frameworks": [ { "name": "next", "version": "^16.1.7" }, { "name": "react", "version": "19.2.4" } ], "databases": [ { "name": "mongodb", "version": "^6.20.0" } ], "betterAuth": { "version": "^1.3.34", "config": { "baseURL": "http://localhost:3002", "basePath": "/api/auth", "emailAndPassword": { "enabled": false }, "socialProviders": { "microsoft": { "clientId": "[REDACTED]", "clientSecret": "[REDACTED]", "tenantId": "common", "authority": "https://login.microsoftonline.com", "prompt": "select_account", "redirectURI": "http://localhost:3002/excalibur/api/auth/callback/microsoft" } }, "advanced": { "database": {} }, "plugins": [ { "name": "next-cookies", "config": { "id": "next-cookies", "hooks": { "after": [ {} ] } } } ] } } } ``` ### Which area(s) are affected? (Select all that apply) Backend, Types, Documentation ### Auth config (if applicable) ```typescript ``` ### Additional context UUIDs are needed for cross-platform compatibility, reverting to ObjectIDs is not suitable in this use-case.
GiteaMirror added the lockedenhancementbug labels 2026-04-15 19:07:55 -05:00
Author
Owner

@bytaesu commented on GitHub (Mar 18, 2026):

Hi @yourlocaltechboi, I'll check this

<!-- gh-comment-id:4085232020 --> @bytaesu commented on GitHub (Mar 18, 2026): Hi @yourlocaltechboi, I'll check this
Author
Owner

@github-actions[bot] commented on GitHub (Mar 31, 2026):

This issue has been locked as it was closed more than 7 days ago. If you're experiencing a similar problem or you have additional context, please open a new issue and reference this one.

<!-- gh-comment-id:4165913648 --> @github-actions[bot] commented on GitHub (Mar 31, 2026): This issue has been locked as it was closed more than 7 days ago. If you're experiencing a similar problem or you have additional context, please open a new issue and reference this one.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#19787