Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is best way to handle global connection of Mongodb in NodeJs

Tags:

I using Node-Mongo-Native and trying to set a global connection variable, but I am confused between two possible solutions. Can you guys help me out with which one would be the good one? 1. Solution ( which is bad because every request will try to create a new connection.)

var express = require('express');   var app = express();   var MongoClient = require('mongodb').MongoClient;   var assert = require('assert');  // Connection URL var url = '[connectionString]]';  // start server on port 3000 app.listen(3000, '0.0.0.0', function() {     // print a message when the server starts listening   console.log("server starting"); });  // Use connect method to connect to the server when the page is requested app.get('/', function(request, response) {     MongoClient.connect(url, function(err, db) {     assert.equal(null, err);     db.listCollections({}).toArray(function(err, collections) {         assert.equal(null, err);         collections.forEach(function(collection) {             console.log(collection);         });         db.close();     })     response.send('Connected - see console for a list of available collections');   }); }); 
  1. Solution ( to connect at app init and assign the connection string to a global variable). but I believe assigning connection string to a global variable is a not a good idea.

    var mongodb; var url = '[connectionString]'; MongoClient.connect(url, function(err, db) {
    assert.equal(null, err); mongodb=db; } );

I want to create a connection at the app initialization and use throughout the app lifetime.

Can you guys help me out? Thanks.

like image 258
NitinD Avatar asked Mar 21 '18 03:03

NitinD


People also ask

Which method is used to connect from NodeJS to MongoDB?

connect() method is the method of the MongoDB module of the Node. js which is used to connect the database with our Node. js Application. This is an asynchronous method of the MongoDB module.

Which drivers are useful to connect NodeJS with MongoDB?

Connect your Node. js applications to MongoDB and work with your data using the Node. js driver. The driver features an asynchronous API that you can use to access method return values through Promises or specify callbacks to access them when communicating with MongoDB.

How does MongoDB connect to NodeJS application?

To create a database in MongoDB, start by creating a MongoClient object, then specify a connection URL with the correct ip address and the name of the database you want to create. MongoDB will create the database if it does not exist, and make a connection to it.

Which method is used to connect to the MongoDB instance using MongoClient?

Connect to a Single MongoDB Instance const MongoClient = require('mongodb'). MongoClient; const assert = require('assert'); // Connection URL const url = 'mongodb://localhost:27017'; // Database Name const dbName = 'myproject'; // Use connect method to connect to the server MongoClient.


1 Answers

Create a Connection singleton module to manage the apps database connection.

MongoClient does not provide a singleton connection pool so you don't want to call MongoClient.connect() repeatedly in your app. A singleton class to wrap the mongo client works for most apps I've seen.

const MongoClient = require('mongodb').MongoClient  class Connection {      static async open() {         if (this.db) return this.db         this.db = await MongoClient.connect(this.url, this.options)         return this.db     }  }  Connection.db = null Connection.url = 'mongodb://127.0.0.1:27017/test_db' Connection.options = {     bufferMaxEntries:   0,     reconnectTries:     5000,     useNewUrlParser:    true,     useUnifiedTopology: true, }  module.exports = { Connection } 

Everywhere you require('./Connection'), the Connection.open() method will be available, as will the Connection.db property if it has been initialised.

const router = require('express').Router() const { Connection } = require('../lib/Connection.js')  // This should go in the app/server setup, and waited for. Connection.open()  router.get('/files', async (req, res) => {    try {      const files = await Connection.db.collection('files').find({})      res.json({ files })    }    catch (error) {      res.status(500).json({ error })    } })  module.exports = router 
like image 69
Matt Avatar answered Sep 23 '22 17:09

Matt