Skip to content

Models

mongo_ops.models

Summary

Base document models for MongoDB.

Classes

BaseDocument

Bases: BaseModel

Base document class with common MongoDB fields.

Inherit from this class to create Pydantic models that represent MongoDB documents. It includes automatic handling of the _id field and timestamps.

Attributes:

Name Type Description
id PyObjectId | None

The MongoDB document ID (aliased to _id).

created_at datetime

Timestamp when the document was created.

updated_at datetime

Timestamp when the document was last updated.

PyObjectId

Bases: ObjectId

Custom ObjectId type compatible with Pydantic v2.

This class extends the standard BSON ObjectId to provide validation and serialization support within Pydantic models.

Functions
__get_pydantic_core_schema__ classmethod
1
2
3
__get_pydantic_core_schema__(
    source_type: Any, handler: GetCoreSchemaHandler
) -> Any

Define the core schema for Pydantic v2 validation and serialization.

__get_pydantic_json_schema__ classmethod
1
2
3
__get_pydantic_json_schema__(
    schema: Any, handler: Any
) -> Any

Update the JSON schema for OpenAPI/Swagger documentation.

validate classmethod
validate(v: Any) -> ObjectId

Validate the input value and convert it to an ObjectId if possible.

Parameters:

Name Type Description Default
v Any

The value to validate (can be str or ObjectId).

required

Returns:

Name Type Description
ObjectId ObjectId

The validated ObjectId instance.

Raises:

Type Description
ValueError

If the value is not a valid ObjectId.