更新时间:2023-10-11 18:40:04
当您的应用程序启动并重用
db对象时,您打开一次MongoClient.connect。它不是一个单独的连接池每个.connect
创建一个新的连接池。
You open do MongoClient.connect once when your app boots up and reuse the db object. It's not a singleton connection pool each .connect creates a new connection pool.
重用由MongoClient.connect()产生的db对象。这给你池,并将提供明显的速度增加与打开/关闭连接每个数据库操作相比。
So, to answer your question directly, reuse the db object that results from MongoClient.connect(). This gives you pooling, and will provide a noticeable speed increase as compared with opening/closing connections on each db action.