Sowmay Jain
Sowmay Jain

Reputation: 955

Is it a good practice to perform actions to database with one single connection to Mongodb?

I'm only using single connection to MongoDB database in a Node-based project. First, declaring a "db" variable and then performing all database related CRUD operations on that single variable or connection.

Is it a good practice or I need to create multiple connections? What will be consequences?

Following is a rough structure:

var db;
MongoClient.connect(url, (err, database) => {
   db = database;
   one(db)
})

function one(db) {
   // doing something with db
   two(db)
}

function two(db) {
   // doing something with db
   three(db)
   five(db)
   six(db)
}

function three(db) {
   // doing something with db
   four(db)
   seven(db)
}

and so on....

Upvotes: 0

Views: 42

Answers (1)

Leo
Leo

Reputation: 844

It is alright to use the same connection to perform all of your queries. Remember that the Mongo Driver for Node.js is asynchronous. That means it will send the queries to the mongod server and continue with the execution of your code without waiting for the results. However, when the server responds with the query results the Mongo Driver will then call your callback function. Therefore all of the heavy workload is on the mongod server not on your node app.

Check out this script that proves this. You can see that everything is done async and the node app can continue with the flow of execution.

var MongoClient = require('mongodb').MongoClient

function testDb(db) {
    var documents = []
    for(var i = 0; i < 100000; i++)
        documents.push({test: 'just testing', exp: [1,2,3]})

    var col = db.collection('cart')

    console.log('insert the 1st one!')

    col.insertMany(documents, {w:1, j:1}, function(err, results) {
        console.log('we inserted the 1st documents')
    })

    console.log('fetch the 2nd one!')

    col.find({}).toArray(function(err, results) {
        console.log('we got the 2nd result' || err)
    })

    console.log('fetch the 3rd one!')

    col.find({}).toArray(function(err, results) {
        console.log('we got the 3rd results' || err)
    })

    console.log('fetch the 4th one!')

    col.find({}).toArray(function(err, results) {
        console.log('we got the 4th results' || err)
    })

    console.log('No more fetches or inserts!')
    console.log('-----------------------------------------')
    console.log('Starting to do some other work!')
    console.log('-----------------------------------------')

    var t = []
    for(var i = 0; i < 100000; i++)
        t.push(i)

    console.log('-----------------------------------------')
    console.log('Done with the extra work!')
    console.log('-----------------------------------------')
}

MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    testDb(db)
});

This is the output after running that node program:

$bash node test.js 
insert the 1st one!
fetch the 2nd one!
fetch the 3rd one!
fetch the 4th one!
No more fetches or inserts!
-----------------------------------------
Starting to do some other work!
-----------------------------------------
-----------------------------------------
Done with the extra work!
-----------------------------------------
we got the 4th results
we got the 3rd results
we got the 2nd result
we inserted the 1st documents

Upvotes: 1

Related Questions