Jsdrive connected Mongodb and was at a loss.

  node.js, question

Using node’s native mongodb driver to connect mongodb, after checking the documents, we found that all the official documents passed through

var MongoClient = require('mongodb').MongoClient;
 // Connection URL
 var url = 'mongodb://localhost:27017/myproject';
 // Use connect method to connect to the Server
 MongoClient.connect(url, function(err, db) {
 if(err) throw err;
 console.log("Connected correctly to server");
 //do something with db
 db.close();
 });

This way to connect the database. How to operate the database in the above way in the actual project? (Write the above code once in every routing function = =)

Note: Previously, the database seemed to be called as follows:

var settings = require('../settings'),
 Db = require('mongodb').Db,
 Connection = require('mongodb').Connection,
 Server = require('mongodb').Server;
 module.exports = new Db(settings.db, new Server(settings.host, settings.port),
 {safe: true});

Then in the module that needs to be used

var db = require('./db');
 db.open(function(err,db){
 //do something
 });

= =, remember that there will be blocked pits, so the official website started to push mongocolient, asking the gathering, how to use mongocolient in the project?

var settings = require('../settings');
 var MongoClient = require('mongodb').MongoClient;
 
 exports.do =  function (func){
 MongoClient.connect(settings.url,function(err,db){
 if(err) throw err;
 else{
 func(db);
 bracket
 });
 };

For the time being.