How do I manage MongoDB connections in a Node.js web application?

前端 未结 11 2317
旧巷少年郎
旧巷少年郎 2020-11-22 02:42

I\'m using the node-mongodb-native driver with MongoDB to write a website.

I have some questions about how to manage connections:

  1. Is it enough using

相关标签:
11条回答
  • 2020-11-22 02:58

    The primary committer to node-mongodb-native says:

    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.

    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.

    0 讨论(0)
  • 2020-11-22 02:58

    If using express there is another more straightforward method, which is to utilise Express's built in feature to share data between routes and modules within your app. There is an object called app.locals. We can attach properties to it and access it from inside our routes. To use it, instantiate your mongo connection in your app.js file.

    var app = express();
    
    MongoClient.connect('mongodb://localhost:27017/')
    .then(client =>{
      const db = client.db('your-db');
      const collection = db.collection('your-collection');
      app.locals.collection = collection;
    });
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  // view engine setup
    app.set('views', path.join(__dirname, 'views'));
    

    This database connection, or indeed any other data you wish to share around the modules of you app can now be accessed within your routes with req.app.locals as below without the need for creating and requiring additional modules.

    app.get('/', (req, res) => {
      const collection = req.app.locals.collection;
      collection.find({}).toArray()
      .then(response => res.status(200).json(response))
      .catch(error => console.error(error));
    });
    

    This method ensures that you have a database connection open for the duration of your app unless you choose to close it at any time. It's easily accessible with req.app.locals.your-collection and doesn't require creation of any additional modules.

    0 讨论(0)
  • 2020-11-22 03:02

    Manage mongo connection pools in a single self contained module. This approach provides two benefits. Firstly it keeps your code modular and easier to test. Secondly your not forced to mix your database connection up in your request object which is NOT the place for a database connection object. (Given the nature of JavaScript I would consider it highly dangerous to mix in anything to an object constructed by library code). So with that you only need to Consider a module that exports two methods. connect = () => Promise and get = () => dbConnectionObject.

    With such a module you can firstly connect to the database

    // runs in boot.js or what ever file your application starts with
    const db = require('./myAwesomeDbModule');
    db.connect()
        .then(() => console.log('database connected'))
        .then(() => bootMyApplication())
        .catch((e) => {
            console.error(e);
            // Always hard exit on a database connection error
            process.exit(1);
        });
    

    When in flight your app can simply call get() when it needs a DB connection.

    const db = require('./myAwesomeDbModule');
    db.get().find(...)... // I have excluded code here to keep the example  simple
    

    If you set up your db module in the same way as the following not only will you have a way to ensure that your application will not boot unless you have a database connection you also have a global way of accessing your database connection pool that will error if you have not got a connection.

    // myAwesomeDbModule.js
    let connection = null;
    
    module.exports.connect = () => new Promise((resolve, reject) => {
        MongoClient.connect(url, option, function(err, db) {
            if (err) { reject(err); return; };
            resolve(db);
            connection = db;
        });
    });
    
    module.exports.get = () => {
        if(!connection) {
            throw new Error('Call connect first!');
        }
    
        return connection;
    }
    
    0 讨论(0)
  • 2020-11-22 03:08

    If you have Express.js, you can use express-mongo-db for caching and sharing the MongoDB connection between requests without a pool (since the accepted answer says it is the right way to share the connection).

    If not - you can look at its source code and use it in another framework.

    0 讨论(0)
  • 2020-11-22 03:10

    Here is some code that will manage your MongoDB connections.

    var MongoClient = require('mongodb').MongoClient;
    var url = require("../config.json")["MongoDBURL"]
    
    var option = {
      db:{
        numberOfRetries : 5
      },
      server: {
        auto_reconnect: true,
        poolSize : 40,
        socketOptions: {
            connectTimeoutMS: 500
        }
      },
      replSet: {},
      mongos: {}
    };
    
    function MongoPool(){}
    
    var p_db;
    
    function initPool(cb){
      MongoClient.connect(url, option, function(err, db) {
        if (err) throw err;
    
        p_db = db;
        if(cb && typeof(cb) == 'function')
            cb(p_db);
      });
      return MongoPool;
    }
    
    MongoPool.initPool = initPool;
    
    function getInstance(cb){
      if(!p_db){
        initPool(cb)
      }
      else{
        if(cb && typeof(cb) == 'function')
          cb(p_db);
      }
    }
    MongoPool.getInstance = getInstance;
    
    module.exports = MongoPool;
    

    When you start the server, call initPool

    require("mongo-pool").initPool();
    

    Then in any other module you can do the following:

    var MongoPool = require("mongo-pool");
    MongoPool.getInstance(function (db){
        // Query your MongoDB database.
    });
    

    This is based on MongoDB documentation. Take a look at it.

    0 讨论(0)
  • 2020-11-22 03:14

    I have been using generic-pool with redis connections in my app - I highly recommend it. Its generic and I definitely know it works with mysql so I don't think you'll have any problems with it and mongo

    https://github.com/coopernurse/node-pool

    0 讨论(0)
提交回复
热议问题