mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2025-08-16 03:40:23 +08:00

* Inject every model variations in router and DB model in DB adapters * Update documentation and import Tortoise in db module * Use path operation decorator dependencies for superuser routes
34 lines
1.1 KiB
Markdown
34 lines
1.1 KiB
Markdown
# MongoDB
|
|
|
|
**FastAPI Users** provides the necessary tools to work with MongoDB databases thanks to [mongodb/motor](https://github.com/mongodb/motor) package for full async support.
|
|
|
|
## Setup database connection and collection
|
|
|
|
Let's create a MongoDB connection and instantiate a collection.
|
|
|
|
```py hl_lines="23 24 25 26"
|
|
{!./src/db_mongodb.py!}
|
|
```
|
|
|
|
You can choose any name for the database and the collection.
|
|
|
|
## Create the database adapter
|
|
|
|
The database adapter of **FastAPI Users** makes the link between your database configuration and the users logic. Create it like this.
|
|
|
|
```py hl_lines="32"
|
|
{!./src/db_mongodb.py!}
|
|
```
|
|
|
|
Notice that we pass a reference to your [`UserDB` model](../model.md).
|
|
|
|
!!! info
|
|
The database adapter will automatically create a [unique index](https://docs.mongodb.com/manual/core/index-unique/) on `id` and `email`.
|
|
|
|
!!! warning
|
|
**FastAPI Users** will use its defined [`id` UUID-string](../model.md) as unique identifier for the user, rather than the builtin MongoDB `_id`.
|
|
|
|
## Next steps
|
|
|
|
We will now configure an [authentication method](../authentication/index.md).
|