MongoDB is a document-oriented NoSQL database. Better Auth provides a first-class MongoDB adapter that maps auth collections to MongoDB documents.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/better-auth/better-auth/llms.txt
Use this file to discover all available pages before exploring further.
Installation
Configuration options
ThemongodbAdapter function accepts a Db instance and an optional config object:
| Option | Type | Description |
|---|---|---|
client | MongoClient | Provide the client to enable multi-document transactions. Omit for standalone MongoDB instances that lack replica sets. |
usePlural | boolean | Use plural collection names (e.g. users instead of user). Defaults to false. |
debugLogs | boolean | Enable verbose adapter logging. Defaults to false. |
transaction | boolean | Wrap multi-step operations in a MongoDB session transaction. Defaults to true when a client is provided. |
Schema and migrations
MongoDB is schemaless — Better Auth creates collections automatically as data is written. There is nogenerate or migrate step required.
Better Auth uses the following collection names by default:
user, session, account, and verification. Collection names match the model names defined in your auth config.Indexes
MongoDB does not enforce uniqueness or index constraints automatically. For optimal performance and data integrity, create the following indexes:Experimental joins
Enabling joins allows Better Auth to use MongoDB$lookup aggregations to fetch related data in a single query instead of multiple round-trips.
auth.ts