[GH-ISSUE #6812] Support MongoDB driver v7 #19269

Closed
opened 2026-04-15 18:08:45 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @abnud11 on GitHub (Dec 17, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/6812

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. put "mongodb": "7.0.0" in package.json
  2. run npm i
  3. use the mongodbAdapter from 'better-auth/adapters/mongodb';
  4. Try to login, you'll see this exception:

2025-12-17T01:23:11.161Z ERROR [Better Auth]: BSONVersionError BSONVersionError: Unsupported BSON version, bson types must be from bson 7.x.x
backend1-1 | at serializeInto (/app/node_modules/.pnpm/bson@7.0.0/node_modules/bson/src/parser/serializer.ts:901:17)
backend1-1 | at serializeObject (/app/node_modules/.pnpm/bson@7.0.0/node_modules/bson/src/parser/serializer.ts:295:20)
backend1-1 | at serializeInto (/app/node_modules/.pnpm/bson@7.0.0/node_modules/bson/src/parser/serializer.ts:663:19)
backend1-1 | at serializeObject (/app/node_modules/.pnpm/bson@7.0.0/node_modules/bson/src/parser/serializer.ts:295:20)
backend1-1 | at serializeInto (/app/node_modules/.pnpm/bson@7.0.0/node_modules/bson/src/parser/serializer.ts:887:19)
backend1-1 | at Object.serialize (/app/node_modules/.pnpm/bson@7.0.0/node_modules/bson/src/bson.ts:108:30)
backend1-1 | at OpMsgRequest.serializeBson (/app/node_modules/.pnpm/mongodb@7.0.0_socks@2.8.4/node_modules/mongodb/src/cmap/commands.ts:617:17)
backend1-1 | at OpMsgRequest.makeSections (/app/node_modules/.pnpm/mongodb@7.0.0_socks@2.8.4/node_modules/mongodb/src/cmap/commands.ts:577:33)
backend1-1 | at OpMsgRequest.toBin (/app/node_modules/.pnpm/mongodb@7.0.0_socks@2.8.4/node_modules/mongodb/src/cmap/commands.ts:559:25)
backend1-1 | at Connection.writeCommand (/app/node_modules/.pnpm/mongodb@7.0.0_socks@2.8.4/node_modules/mongodb/src/cmap/connection.ts:699:53)
backend1-1 | # SERVER_ERROR: BSONVersionError: Unsupported BSON version, bson types must be from bson 7.x.x
backend1-1 | at serializeInto (/app/node_modules/.pnpm/bson@7.0.0/node_modules/bson/src/parser/serializer.ts:901:17)
backend1-1 | at serializeObject (/app/node_modules/.pnpm/bson@7.0.0/node_modules/bson/src/parser/serializer.ts:295:20)
backend1-1 | at serializeInto (/app/node_modules/.pnpm/bson@7.0.0/node_modules/bson/src/parser/serializer.ts:663:19)
backend1-1 | at serializeObject (/app/node_modules/.pnpm/bson@7.0.0/node_modules/bson/src/parser/serializer.ts:295:20)
backend1-1 | at serializeInto (/app/node_modules/.pnpm/bson@7.0.0/node_modules/bson/src/parser/serializer.ts:887:19)
backend1-1 | at Object.serialize (/app/node_modules/.pnpm/bson@7.0.0/node_modules/bson/src/bson.ts:108:30)
backend1-1 | at OpMsgRequest.serializeBson (/app/node_modules/.pnpm/mongodb@7.0.0_socks@2.8.4/node_modules/mongodb/src/cmap/commands.ts:617:17)
backend1-1 | at OpMsgRequest.makeSections (/app/node_modules/.pnpm/mongodb@7.0.0_socks@2.8.4/node_modules/mongodb/src/cmap/commands.ts:577:33)
backend1-1 | at OpMsgRequest.toBin (/app/node_modules/.pnpm/mongodb@7.0.0_socks@2.8.4/node_modules/mongodb/src/cmap/commands.ts:559:25)
backend1-1 | at Connection.writeCommand (/app/node_modules/.pnpm/mongodb@7.0.0_socks@2.8.4/node_modules/mongodb/src/cmap/connection.ts:699:53)

Current vs. Expected behavior

Current Behavior:
It throws BSONVersionError error

Expected Behavior:
Login should work regardless of mongodb driver version.

What version of Better Auth are you using?

1.4.7

System info

{
  "system": {
    "platform": "linux",
    "arch": "x64",
    "version": "#1 SMP PREEMPT_DYNAMIC Mon, 24 Nov 2025 15:21:09 +0000",
    "release": "6.17.9-arch1-1",
    "cpuCount": 16,
    "cpuModel": "AMD Ryzen 7 7840HS w/ Radeon 780M Graphics",
    "totalMemory": "22.71 GB",
    "freeMemory": "14.92 GB"
  },
  "node": {
    "version": "v24.12.0",
    "env": "development"
  },
  "packageManager": {
    "name": "npm",
    "version": "11.6.2"
  },
  "frameworks": null,
  "databases": null,
  "betterAuth": {
    "version": "^1.4.7",
    "config": null
  }
}

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

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
  database: mongodbAdapter(
    db as unknown as Parameters<typeof mongodbAdapter>[0],
  ),
});

Additional context

No response

Originally created by @abnud11 on GitHub (Dec 17, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/6812 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. put `"mongodb": "7.0.0"` in `package.json` 2. run `npm i` 3. use the mongodbAdapter from `'better-auth/adapters/mongodb'`; 4. Try to login, you'll see this exception: > 2025-12-17T01:23:11.161Z ERROR [Better Auth]: BSONVersionError BSONVersionError: Unsupported BSON version, bson types must be from bson 7.x.x > backend1-1 | at serializeInto (/app/node_modules/.pnpm/bson@7.0.0/node_modules/bson/src/parser/serializer.ts:901:17) > backend1-1 | at serializeObject (/app/node_modules/.pnpm/bson@7.0.0/node_modules/bson/src/parser/serializer.ts:295:20) > backend1-1 | at serializeInto (/app/node_modules/.pnpm/bson@7.0.0/node_modules/bson/src/parser/serializer.ts:663:19) > backend1-1 | at serializeObject (/app/node_modules/.pnpm/bson@7.0.0/node_modules/bson/src/parser/serializer.ts:295:20) > backend1-1 | at serializeInto (/app/node_modules/.pnpm/bson@7.0.0/node_modules/bson/src/parser/serializer.ts:887:19) > backend1-1 | at Object.serialize (/app/node_modules/.pnpm/bson@7.0.0/node_modules/bson/src/bson.ts:108:30) > backend1-1 | at OpMsgRequest.serializeBson (/app/node_modules/.pnpm/mongodb@7.0.0_socks@2.8.4/node_modules/mongodb/src/cmap/commands.ts:617:17) > backend1-1 | at OpMsgRequest.makeSections (/app/node_modules/.pnpm/mongodb@7.0.0_socks@2.8.4/node_modules/mongodb/src/cmap/commands.ts:577:33) > backend1-1 | at OpMsgRequest.toBin (/app/node_modules/.pnpm/mongodb@7.0.0_socks@2.8.4/node_modules/mongodb/src/cmap/commands.ts:559:25) > backend1-1 | at Connection.writeCommand (/app/node_modules/.pnpm/mongodb@7.0.0_socks@2.8.4/node_modules/mongodb/src/cmap/connection.ts:699:53) > backend1-1 | # SERVER_ERROR: BSONVersionError: Unsupported BSON version, bson types must be from bson 7.x.x > backend1-1 | at serializeInto (/app/node_modules/.pnpm/bson@7.0.0/node_modules/bson/src/parser/serializer.ts:901:17) > backend1-1 | at serializeObject (/app/node_modules/.pnpm/bson@7.0.0/node_modules/bson/src/parser/serializer.ts:295:20) > backend1-1 | at serializeInto (/app/node_modules/.pnpm/bson@7.0.0/node_modules/bson/src/parser/serializer.ts:663:19) > backend1-1 | at serializeObject (/app/node_modules/.pnpm/bson@7.0.0/node_modules/bson/src/parser/serializer.ts:295:20) > backend1-1 | at serializeInto (/app/node_modules/.pnpm/bson@7.0.0/node_modules/bson/src/parser/serializer.ts:887:19) > backend1-1 | at Object.serialize (/app/node_modules/.pnpm/bson@7.0.0/node_modules/bson/src/bson.ts:108:30) > backend1-1 | at OpMsgRequest.serializeBson (/app/node_modules/.pnpm/mongodb@7.0.0_socks@2.8.4/node_modules/mongodb/src/cmap/commands.ts:617:17) > backend1-1 | at OpMsgRequest.makeSections (/app/node_modules/.pnpm/mongodb@7.0.0_socks@2.8.4/node_modules/mongodb/src/cmap/commands.ts:577:33) > backend1-1 | at OpMsgRequest.toBin (/app/node_modules/.pnpm/mongodb@7.0.0_socks@2.8.4/node_modules/mongodb/src/cmap/commands.ts:559:25) > backend1-1 | at Connection.writeCommand (/app/node_modules/.pnpm/mongodb@7.0.0_socks@2.8.4/node_modules/mongodb/src/cmap/connection.ts:699:53) ### Current vs. Expected behavior Current Behavior: It throws BSONVersionError error Expected Behavior: Login should work regardless of mongodb driver version. ### What version of Better Auth are you using? 1.4.7 ### System info ```bash { "system": { "platform": "linux", "arch": "x64", "version": "#1 SMP PREEMPT_DYNAMIC Mon, 24 Nov 2025 15:21:09 +0000", "release": "6.17.9-arch1-1", "cpuCount": 16, "cpuModel": "AMD Ryzen 7 7840HS w/ Radeon 780M Graphics", "totalMemory": "22.71 GB", "freeMemory": "14.92 GB" }, "node": { "version": "v24.12.0", "env": "development" }, "packageManager": { "name": "npm", "version": "11.6.2" }, "frameworks": null, "databases": null, "betterAuth": { "version": "^1.4.7", "config": null } } ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, database: mongodbAdapter( db as unknown as Parameters<typeof mongodbAdapter>[0], ), }); ``` ### Additional context _No response_
GiteaMirror added the lockedbug labels 2026-04-15 18:08:46 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Dec 17, 2025):

(tag @better-auth-agent if you would like an answer from the Agent)

If you need more help, tag @better-auth-agent in a comment so I can respond.

DiagramDiscordGitHub

Diagram Join Star

<!-- gh-comment-id:3663170681 --> @better-auth-agent[bot] commented on GitHub (Dec 17, 2025): (tag @better-auth-agent if you would like an answer from the Agent) _If you need more help, tag @better-auth-agent in a comment so I can respond._ <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) • [Discord](https://discord.gg/fG2XXEuQX3) • [GitHub](https://github.com/Skyvern-AI/Skyvern) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/fG2XXEuQX3) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/Skyvern-AI/Skyvern)
Author
Owner

@dosubot[bot] commented on GitHub (Dec 17, 2025):

Better Auth 1.4.7 does not officially support MongoDB driver v7. The BSONVersionError happens because the mongodbAdapter tightly couples to the ObjectId class from the 'mongodb' package, and version mismatches between the adapter and the installed MongoDB/BSON libraries cause serialization errors. The current peer dependency is set to ^6.18.0, so v7 is not yet supported see package.json.

Workarounds: Most users have resolved this by downgrading to MongoDB driver 6.x (e.g. 6.21.0) see user report. Maintainers currently recommend using driver version 6.8 or lower for compatibility see maintainer comment.

Official support for MongoDB 7 is planned but not yet released—there are open pull requests updating dependencies and preparing for v7, but nothing merged or published yet see PR. Supporting v7 will require updating peer dependencies and refactoring how ObjectId/BSON types are handled in the adapter.

If you must use MongoDB 7, you could try patching your local adapter to ensure all ObjectId/BSON usage matches the installed driver version, but this is not officially supported and may require significant changes. For now, the safest path is to use MongoDB driver 6.x and monitor the open PRs for updates.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

<!-- gh-comment-id:3663178548 --> @dosubot[bot] commented on GitHub (Dec 17, 2025): <!-- Answer --> Better Auth 1.4.7 does not officially support MongoDB driver v7. The BSONVersionError happens because the mongodbAdapter tightly couples to the ObjectId class from the 'mongodb' package, and version mismatches between the adapter and the installed MongoDB/BSON libraries cause serialization errors. The current peer dependency is set to ^6.18.0, so v7 is not yet supported [see package.json](https://github.com/better-auth/better-auth/blob/1a9b1a3e66dda283ec8bc9f80145124a34eb37fa/packages/better-auth/package.json#L475-L551). Workarounds: Most users have resolved this by downgrading to MongoDB driver 6.x (e.g. 6.21.0) [see user report](https://github.com/better-auth/better-auth/issues/4741). Maintainers currently recommend using driver version 6.8 or lower for compatibility [see maintainer comment](https://github.com/better-auth/better-auth/issues/6124). Official support for MongoDB 7 is planned but not yet released—there are open pull requests updating dependencies and preparing for v7, but nothing merged or published yet [see PR](https://github.com/better-auth/better-auth/pull/6596). Supporting v7 will require updating peer dependencies and refactoring how ObjectId/BSON types are handled in the adapter. If you must use MongoDB 7, you could try patching your local adapter to ensure all ObjectId/BSON usage matches the installed driver version, but this is not officially supported and may require significant changes. For now, the safest path is to use MongoDB driver 6.x and monitor the open PRs for updates. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/2d11a8cc-f0d1-4e87-befd-988bf5b270bf?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/2d11a8cc-f0d1-4e87-befd-988bf5b270bf?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/2d11a8cc-f0d1-4e87-befd-988bf5b270bf?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/2d11a8cc-f0d1-4e87-befd-988bf5b270bf?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/2d11a8cc-f0d1-4e87-befd-988bf5b270bf?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/2d11a8cc-f0d1-4e87-befd-988bf5b270bf?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/2d11a8cc-f0d1-4e87-befd-988bf5b270bf?feedback_type=other)</sup> [![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=Better%20Auth%201.4.7%20does%20not%20officially%20support%20MongoDB%20driver%20v7.%20The%20BSONVersionError%20happens%20because%20the%20mongodbAdapter%20tightly%20couples%20to%20the%20ObjectId%20class%20from%20the%20%27mongodb%27%20package%2C%20and%20version%20mismatches%20between%20the%20adapter%20and%20the%20installed%20MongoDB/BSON%20libraries%20cause%20serialization%20errors.%20The%20current%20peer%20dependency%20is%20set%20to%20%5E6.18.0%2C%20so%20v7%20is%20not%20yet%20supported%C2%A0%5Bsee%20package.json%5D%28https%3A//github.com/better-auth/better-auth/blob/1a9b1a3e66dda283ec8bc9f80145124a34eb37fa/packages/better-auth/package.json%23L475-L551%29.%0A%0AWorkarounds%3A%20Most%20users%20have%20resolved%20this%20by%20downgrading%20to%20MongoDB%20driver%206.x%20%28e.g.%206.21.0%29%C2%A0%5Bsee%20user%20report%5D%28https%3A//github.com/better-auth/better-auth/issues/4741%29.%20Maintainers%20currently%20recommend%20using%20driver%20version%206.8%20or%20lower%20for%20compatibility%C2%A0%5Bsee%20maintainer%20comment%5D%28https%3A//github.com/better-auth/better-auth/issues/6124%29.%0A%0AOfficial%20support%20for%20MongoDB%207%20is%20planned%20but%20not%20yet%20released%E2%80%94there%20are%20open%20pull%20requests%20updating%20dependencies%20and%20preparing%20for%20v7%2C%20but%20nothing%20merged%20or%20published%20yet%C2%A0%5Bsee%20PR%5D%28https%3A//github.com/better-auth/better-auth/pull/6596%29.%20Supporting%20v7%20will%20require%20updating%20peer%20dependencies%20and%20refactoring%20how%20ObjectId/BSON%20types%20are%20handled%20in%20the%20adapter.%0A%0AIf%20you%20must%20use%20MongoDB%207%2C%20you%20could%20try%20patching%20your%20local%20adapter%20to%20ensure%20all%20ObjectId/BSON%20usage%20matches%20the%20installed%20driver%20version%2C%20but%20this%20is%20not%20officially%20supported%20and%20may%20require%20significant%20changes.%20For%20now%2C%20the%20safest%20path%20is%20to%20use%20MongoDB%20driver%206.x%20and%20monitor%20the%20open%20PRs%20for%20updates.)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/6812)
Author
Owner

@github-actions[bot] commented on GitHub (Apr 2, 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:4173718220 --> @github-actions[bot] commented on GitHub (Apr 2, 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#19269