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 |
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
Define the core schema for Pydantic v2 validation and serialization.
__get_pydantic_json_schema__
classmethod
Update the JSON schema for OpenAPI/Swagger documentation.
validate
classmethod
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. |