FastAPI CRUD MongoDB is a lightweight REST API starter that demonstrates full create, read, update, and delete operations over two resources — Users and Tasks — using the modern Python web framework FastAPI, the official MongoDB driver PyMongo, and Pydantic for data validation. The project is designed to be simple and readable, making it an ideal reference for developers learning how to wire FastAPI to a MongoDB backend without the overhead of an ODM like Beanie or MongoEngine.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 must be installed and running locally before starting the server. The
application connects to the default host
localhost on port 27017 with no
authentication required. Download MongoDB Community Edition at
mongodb.com/try/download/community.Resources
The API manages two top-level resources, each backed by its own MongoDB collection.User
Defined inapp/models/user.py using Pydantic’s BaseModel:
| Field | Type | Required | Description |
|---|---|---|---|
id | string | No | MongoDB ObjectId serialised as a string. Auto-assigned on creation. |
name | string | Yes | Display name of the user. |
age | integer | Yes | Age of the user. |
email | string | Yes | Email address of the user. |
Task
Defined inapp/models/task.py. A separate TaskUpdate model makes every field optional so partial updates are safe:
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
id | string | No | — | MongoDB ObjectId serialised as a string. |
title | string | Yes | — | Short title of the task. |
description | string | Yes | — | Detailed description of the task. |
completed | boolean | No | false | Whether the task has been completed. |
created_by | string | Yes | — | Name or identifier of the task owner. |
Project Layout
config/— infrastructure concerns: the database connection and OpenAPI metadata live here, keeping them separate from application code.models/— Pydantic models that serve as both request bodies and response schemas. FastAPI uses these for automatic validation and serialisation.schemas/— plain functions that convert raw MongoDB documents (which use_id) into API-friendly dicts (which useid).controllers/— the business-logic layer. Routes delegate all database interaction to controller classes, keeping route handlers thin.routes/—APIRouterinstances grouped by tag. Each router is registered inmain.pyviaapp.include_router(...).
Application Entry Point
main.py creates the FastAPI application, wires the OpenAPI metadata, and registers all three routers:
api_tags is defined in app/config/docs.py and provides human-readable descriptions for each tag group shown in the interactive docs:
Database Connection
app/config/db.py initialises a PyMongo MongoClient with no arguments, which connects to localhost:27017 by default:
mong_conn directly and access collections through it — for example, mong_conn.local.user for the users collection and mong_conn.local.task for tasks. Both collections live in MongoDB’s built-in local database.
Interactive API Documentation
FastAPI automatically generates two interactive documentation UIs at runtime — no extra configuration required:| UI | URL | Description |
|---|---|---|
| Swagger UI | /docs | Try every endpoint directly from the browser. |
| ReDoc | /redoc | Clean, read-only reference layout. |
Dependencies
The project intentionally keeps its dependency footprint minimal:| Package | Version | Purpose |
|---|---|---|
fastapi[standard] | 0.122.0 | Web framework, validation, and OpenAPI support. |
pymongo | 4.15.4 | Official MongoDB driver for Python. |
fastapi[standard] bundles Pydantic v2, Starlette, and Uvicorn so no additional runtime packages are needed.
Next Steps
Quickstart
Install dependencies, start the server, and make your first API call in under five minutes.
API Reference
Explore every endpoint, request body, and response schema for Users and Tasks.