Friends are learning how to connect to mongodb over fastapi using the async motor driver.
I’ve highlighted two ways to connect to mongodb that people use.
- one generic client to mongodb (for example, when running an application).
- For each request, create an AsyncIOMotorClient() client usually in
@app.on_event(“startup”)
And close it in
@app.on_event(“shutdown”).
What is the correct and most economical way to connect to async(motor) mongodb. and is it necessary to close the connection?