如何在 Python 的 MongoDB 中添加我自己的 _id 而不是已经给定的 _id?

问题描述 投票:0回答:0

我有一个使用 Pydantics 的类模型。我尝试提供我自己的 ID,但它在 MongoDB 数据库中为我提供了两个 id 字段。一个是我给它的,一个是它自动做的。

here is the result of my post method

这是我的课:

class ArticleModel(BaseModel):
    _id: int
    title: str
    body: str
    tags: Optional[list] = None
    datetime: Optional[datetime] = None
    caption: Optional[str] = None
    link: Optional[str] = None

class Config:
    orm_mode = True
    allow_population_by_field_name = True
    arbitrary_types_allowed = True`
python mongodb pymongo fastapi pydantic
© www.soinside.com 2019 - 2024. All rights reserved.