When to close MongoDB database connection in Nodejs

前端 未结 8 637
一整个雨季
一整个雨季 2020-11-28 21:56

Working with Nodejs and MongoDB through Node MongoDB native driver. Need to retrieve some documents, and make modification, then save them right back. This is an example:

8条回答
  •  情话喂你
    2020-11-28 22:32

    It's best to use a pooled connection and then call db.close() in cleanup function at the end of your application's life:

    process.on('SIGINT', cleanup);
    process.on('SIGTERM', cleanup);
    

    See http://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html

    A bit old thread, but anyway.

提交回复
热议问题