コード例 #1
0
def create_article(article_input: ArticleInput) -> Article:

    collection = Article.collection()
    document = article_input.dict()

    resp = collection.insert_one(document)
    inserted_id = resp.inserted_id

    document["_id"] = inserted_id

    return Article.from_mongodb(document)
コード例 #2
0
def get_article_by_id(article_id: Union[str, ObjectId]) -> Optional[Article]:

    article_id = ensure_object_id(article_id)

    collection = Article.collection()
    found = collection.find_one(article_id)

    if not found:
        return None

    return Article.from_mongodb(found)