MongoObjectId

The MongoObjectId is a subclass of the bson ObjectId class. It is used to define the id field of your MongoModel. It is also used to generate the OpenAPI schema for your routes.

The only goal behing it, is to make it work like it should be used by MongoDb 🌱.

Use MongoObjectId as type hint for your id field

...
class MyModel(MongoModel):
    id: Annotated[ObjectId, MongoObjectId] | None = None

You can also use it as a foreign field for your embed or lookup models.

Use MongoObjectId as type hint for your foreign fields

...
class MyModel(MongoModel):
    id: Annotated[ObjectId, MongoObjectId] | None = None
    user_id: Annotated[ObjectId, MongoObjectId]
The optional field is mainly used to let the field being generated by the database, but in a lookup case it have to be defined so it shouldn't be Optional.