Skip to main content

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.

The PUT /tasks/{id} endpoint performs a partial update on an existing task document. The request body is validated against the TaskUpdate model, where every field is Optional. Before the update is sent to MongoDB, the task_update_entity() helper strips any None-valued keys from the dict so that only the explicitly provided fields are passed to MongoDB’s $set operator. The endpoint then re-fetches the document and returns the full updated task object. PUT /tasks/{id}

Path Parameters

id
string
required
MongoDB ObjectId expressed as a 24-character hex string (e.g. 64a1b2c3d4e5f6789abcde01) identifying the task to update.

Request Body

All fields are optional. Include only the fields you want to change.
title
string
New title for the task.
description
string
New description for the task.
completed
boolean
Updated completion status of the task.
created_by
string
Updated user identifier associated with the task.

Example Request — Partial Update

Mark a task as completed without touching any other fields:
curl -X PUT http://localhost:5000/tasks/64a1b2c3d4e5f6789abcde01 \
  -H "Content-Type: application/json" \
  -d '{"completed": true}'

Example Request Body — Partial

{
  "completed": true
}

Example Request Body — Full

{
  "title": "Buy groceries",
  "description": "Milk, eggs, bread, butter",
  "completed": true,
  "created_by": "alice@example.com"
}

Example Response

{
  "id": "64a1b2c3d4e5f6789abcde01",
  "title": "Buy groceries",
  "description": "Milk, eggs, bread, butter",
  "completed": true,
  "created_by": "alice@example.com"
}

Response Fields

id
string
MongoDB ObjectId serialised as a 24-character hex string.
title
string
The task title after the update.
description
string
The task description after the update.
completed
boolean
The completion status after the update.
created_by
string
The user identifier after the update.
Unlike a typical full-replacement PUT, this endpoint uses the TaskUpdate model where all fields are Optional. The task_update_entity() function iterates over the dict and removes any key whose value is None, so only fields that are explicitly provided (and non-None) are forwarded to MongoDB’s $set operator. Fields whose value is None are stripped and left unchanged in MongoDB.
The completed field in TaskUpdate has a model-level default of false — not None. This means that if you omit completed from the request body entirely, Pydantic will populate it as false before task_update_entity() runs. Because false is not None, it will not be stripped, and MongoDB will overwrite the task’s completed value with false. Always include completed explicitly in your request body if you intend to set it to true, or if you want the existing value preserved.

Error Responses

StatusDetailCause
404 Not Found"Task Not Found!"No task document exists in MongoDB with the provided id.
422 Unprocessable EntityValidation error detailsA provided field has an incompatible type (e.g. passing a string for completed).
500 Internal Server ErrorServer error messageDatabase connection failure or malformed ObjectId string.
Supplying a malformed ObjectId (not a valid 24-character hex string) will cause bson.errors.InvalidId to be raised, resulting in a 500 error. Always use an id obtained from a previous create or list response.

Build docs developers (and LLMs) love