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 /users/{id} endpoint updates an existing user document identified by its MongoDB ObjectId. The controller calls find_one_and_update with a $set operator, replacing the stored name, age, and email fields with the values provided in the request body. After a successful update, the controller re-fetches the document and returns the latest state. If no document matches the given id, the API returns HTTP 404 Not Found. PUT /users/{id}

Path Parameters

id
string
required
MongoDB ObjectId (24-character hex string) identifying the user document to update. Example: 64a1b2c3d4e5f6789abcdef0.

Request Body

name
string
required
The user’s updated full name.
age
integer
required
The user’s updated age.
email
string
required
The user’s updated email address.
PUT replaces all fields via MongoDB’s $set operator — all three fields (name, age, email) must be provided in every request. Omitting a field will cause a Pydantic validation error since all three are required by the User model.

Response Fields

id
string
The MongoDB ObjectId serialized as a 24-character hexadecimal string.
name
string
The user’s updated full name.
age
integer
The user’s updated age.
email
string
The user’s updated email address.

Example Request

curl -X PUT http://localhost:5000/users/64a1b2c3d4e5f6789abcdef0 \
  -H "Content-Type: application/json" \
  -d '{"name": "Alice Updated", "age": 31, "email": "alice_new@example.com"}'

Request Body

{
  "name": "Alice Updated",
  "age": 31,
  "email": "alice_new@example.com"
}

Example Response

A successful request returns HTTP 200 OK with the full updated user document.
{
  "id": "64a1b2c3d4e5f6789abcdef0",
  "name": "Alice Updated",
  "age": 31,
  "email": "alice_new@example.com"
}

Error Responses

404 Not Found

Returned when no user document exists for the provided id.
{
  "detail": "User Not Found!"
}
Passing a malformed ObjectId string (not a valid 24-character hex value) will cause a bson.errors.InvalidId exception before the database is queried. Ensure id is a properly formatted ObjectId.

Build docs developers (and LLMs) love