使用FastAPI的MongoDb [英] MongoDb with FastAPI

查看:2087
本文介绍了使用FastAPI的MongoDb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩 FastAPI ,并希望将其连接到MongoDB数据库.但是,我感到困惑的是,在异步电动机和mongoengine电动机之间选择哪种ODM.另外,在NoSQL示例此处中,他们创建了一个新存储桶,每次使用时连接到db的代码.但是,电机和mongoengine似乎都更喜欢全局连接.那么连接到mongodb的好方法是什么?

I am playing around with FastAPI a bit and wanted to connect it to a MongoDB database. I however am confused which ODM to choose between motor which is async and mongoengine. Also, in the NoSQL example here they have created a new bucket and also the called the code to connect to db every time it is used. However, both motor and mongoengine seem to prefer a global connection. So what would be a good way to connect to mongodb?

推荐答案

我相信您已经在Github上的Fastapi项目的问题论坛中得到了答案:

I believe you already got your answers in the issue forums of the Fastapi project on Github: Issue 452 (closed). But I'll recap the solutions here for future reference:

简而言之,您可以使用 motor

In short, you can use either motor or mongoengine, Fastapi supports both and you can reuse a global client object that's started and ended with your app process.

一些上下文细节(希望)阐明了这些技术及其关系:

Some context details to (hopefully) clarify these technologies and their relationships:

Python的官方MongoDB驱动程序为 pymongo .在引擎盖下,MongoEngine和Motor都使用Pymongo. Pymongo为MongoDB(守护程序)实现了直接客户端,并提供了Python API来发出请求.

The official MongoDB driver for Python is pymongo. Under the hoods, both MongoEngine and Motor use Pymongo. Pymongo implements a direct client for MongoDB (daemons) and offers a Python API to make requests.

如果愿意,可以直接将pymongo与Fastapi一起使用. (在SQL方面,这相当于直接在Flask中使用psycopg2,而无需经历类似SQLAlchemy的事情.)

If you wanted to, you could use pymongo with Fastapi directly. (On th SQL side of things, this would be equivalent to using psycopg2 in Flask directly without going through something like SQLAlchemy.)

MongoEngine是ODM(对象文档映射器).它提供了一个Python的面向对象的API,您可以在应用程序中使用它来更舒适地工作,并且在涉及实际的数据库请求时,MongoEngine将使用pymongo.

MongoEngine is an ODM (Object-Document Mapper). It offers a Python object-oriented API that you can use in your application to work more comfortably and when it comes to the actual DB requests, MongoEngine will use pymongo.

Motor是pymongo的包装器,使其无阻塞(允许异步/等待).它通过龙卷风或通过异步使用事件循环.如果您将Fastapi与uvicorn一起使用,则uvicorn将通过uvloop实现异步功能.简而言之,将Motor与FastAPI一起使用,异步应该正常工作".不幸的是,Motor没有实现ODM.从这个意义上讲,它更类似于pymongo.

Motor is a wrapper for pymongo that makes it non-blocking (allowing async/await). It uses an event-loop, either through Tornado or through asyncio. If you are using Fastapi with uvicorn, uvicorn will implement async functionality with uvloop. In short, using Motor with FastAPI, async should "just work". Unfortunately, Motor does not implement an ODM. In this sense it is more similar to pymongo.

Fastapi(使用Starlette)处理来自客户端的请求,但是它将使您实现自己的与MongoDB的连接.因此,您不受任何特定选择的限制,但是您大多是自己一个人(la Flask).

Fastapi handles the requests from clients (using Starlette), but it will let you implement your own connection to MongoDB. So you are not restricted to any particular choice, but you are mostly on your own (a la Flask).

您可以使用FastAPI应用程序的启动/关闭挂钩来启动/停止Motor/MongoEngine客户端.您无需担心由于多进程问题而导致客户端对象不持久,因为Fastapi是单线程的.

You can use the startup/shutdown hooks of your FastAPI app to start/stop your Motor/MongoEngine client. You don't need to worry about your client object not persisting due to multiprocess issues, because Fastapi is single-threaded.

@app.on_event("startup")
async def create_db_client():
    # start client here and reuse in future requests


@app.on_event("shutdown")
async def shutdown_db_client():
    # stop your client here

可以在此处中找到使用Fastapi实现电机的示例.

An example implementation of motor with Fastapi can be found here.

这篇关于使用FastAPI的MongoDb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆