What is the correct and most economical way to connect to async(motor) mongodb. and is it necessary to close the connection?

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.

  1. one generic client to mongodb (for example, when running an application).
  2. 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?

Hi @Kaper_Di and welcome to the forums!

Please have a look at a similar post below, and see whether this answer your question

Regards,
Wan.

1 Like