简体   繁体   中英

How mongodb connection works on concurrent requests in NodeJS express server?

I am new to mongoDB and I'm currently working on setting it up with Node express server. I wonder how to manage concurrent requests to the mongodb to read the collection data using the mongoose driver module.

For example:

If 100 users are accessing my server at a time ( http://xxxxxx.com/showusers ), how will the mongodb connection in the express server work? Will it be a single connection or split into 100 connections, one for each request?

How can I close the connection object in mongodb efficiently after the operation? Or can we leave the connection in the express server as in the below code?

Here follows my code..

Server.js

var express = require('express');

var app = express();
app.set('port', config.port);



  app.get('/users',storeusersapi.showUsers);

  app.get('/storeUser',storeusersapi._insertUserDetails);

  app.get('/findUser/:email',storeusersapi._findUser);

app.listen(app.get('port'),function(){
   log.info('Express app started on port ' + app.get('port'));
});

storeusersapi.js

    var mongoose = require('mongoose');
    var log = require('../config/logger');

// Mongoose connection to MongoDB (ted/ted is readonly)
mongoose.connect('mongodb://localhost/mydb', function (error) {
if (error) {
    log.error("MongoDB Connection failure - " +error);
}else{
    log.info('MongoDB is connected Successfully!!!');
}
});

// Mongoose Schema definition
var Schema = mongoose.Schema;
var UserSchema = new Schema({
first_name: String,
last_name: String,
email: String
});

// Mongoose Model definition
var User = mongoose.model('users', UserSchema);

exports.showUsers = function(req,res){
User.find({}, function (err, docs) {
    res.json(docs);
});
};

exports._insertUserDetails = function(req,res){
   var object = new User({first_name:'bob',last_name:'sel',email:'sel@xxxxx.com'});

object.save(function (err) {
    if (err) {
        log.error('Insertion error - '+ err);
    }
    else {
        log.info("User Stored into database!!!");
    }
});




};

exports._findUser = function(req,res){
User.find({ email: req.params.email }, function (err, docs) {
    res.json(docs);
});

};

I have answered for both of your question separately.

1. How will the mongodb connection in the express server work?

Once a connection is created to the mongodb database.(using mongoose or any other framework) It will create a pool of connections with that. (Mongoose default pool size is 5, 100 in python) The created connection pool is maintained by the driver therefore those connections can be re-used when connections to the database are required.

The best practice is to create a new connection only once for the whole application. Once connection is created the connection object will be used as a singleton. When you connect to the database using mongoose models, separate connections are allocated from the created connection pool.

If you are going to create a new connection each time then It will cause to a connection churn.

2. How can I close the connection object in mongodb efficiently after the operation ?

I am not sure 100% about this answer. My suggestion is to disconnect the connection when the express application exits.

var db = mongoose.connect('mongodb://localhost:27017/database');    
db.disconnect();

According to my knowledge what you have don in the code is correct. You have created a new connection only once. Since the connection pool is created with that you don't need to create more connections.

Please go through this link to get a clear understanding on connection pools and their usage. https://dzone.com/articles/deep-dive-connection-pooling

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM