This project exposes three main areas of configuration: the MongoDB connection (where and how the app connects to your database), the FastAPI app metadata (title, version, and Swagger UI groupings), and the uvicorn server startup options (port, hot-reload, and host binding). All three are handled through a small number of files that are straightforward to adjust before deploying to a new environment.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/tutosrive/fastapi-CRUD-MongoDB/llms.txt
Use this file to discover all available pages before exploring further.
MongoDB Connection
The entire database connection is defined in a single line insideapp/config/db.py:
MongoClient() with no arguments is equivalent to MongoClient("mongodb://localhost:27017/"). This default is fine for local development where MongoDB is installed and running on the same machine.
The application accesses two collections through this connection object:
| Collection reference | Database | Collection |
|---|---|---|
mong_conn.local.user | local | user |
mong_conn.local.task | local | task |
Connecting to a Remote Host
To point the app at a remote MongoDB server, replace the no-argument call with a full connection URI:MongoClient call, also update the database name in every controller that references mong_conn.local.* — for example, change mong_conn.local.user to mong_conn.myapp.user.
FastAPI App Metadata
TheFastAPI() constructor in main.py accepts metadata that appears in the auto-generated OpenAPI schema and Swagger UI:
| Parameter | Current value | Purpose |
|---|---|---|
title | 'FASTAPI + MongoDB' | Displayed as the API name in Swagger UI and ReDoc |
description | 'Learning FastAPI with MongoDB ...' | Shown below the title in the API docs |
version | '2025.0.1.0' | API version string in the OpenAPI spec |
openapi_tags | api_tags | Controls endpoint grouping in Swagger UI |
api_tags list is imported from app/config/docs.py:
api_tags corresponds to a tags value set on an APIRouter. When a router declares tags=['User'], every endpoint registered on that router is grouped under the User section in Swagger UI with the description text from this list. Add a new entry here whenever you introduce a new resource router.
Running the Server
The application is served by uvicorn, an ASGI server. Run it from the project root directory (the folder containingmain.py).
Basic start (default port 8000):
| Flag | Effect |
|---|---|
--reload | Watches source files for changes and automatically restarts the server. Use only during development — it adds overhead. |
--port 5000 | Binds the server to port 5000 instead of the default 8000. |
--host 0.0.0.0 | Binds to all network interfaces, making the server accessible from other machines on the same network. |
Interactive API Docs
FastAPI automatically generates two interactive documentation interfaces at no extra configuration cost:Swagger UI
Available at
/docs. Provides a full interactive interface where you can inspect every endpoint, view request/response schemas, and execute live requests directly from the browser.ReDoc
Available at
/redoc. Renders a clean, read-only reference view of the OpenAPI spec — useful for sharing API documentation with consumers who do not need to run requests.