简体   繁体   English

mongodb连接如何处理NodeJS express服务器中的并发请求?

[英]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. 我是mongoDB的新手,我目前正致力于使用Node express服务器进行设置。 I wonder how to manage concurrent requests to the mongodb to read the collection data using the mongoose driver module. 我想知道如何使用mongoose驱动程序模块管理对mongodb的并发请求以读取集合数据。

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? 如果一次有100个用户访问我的服务器( http://xxxxxx.com/showusers ),快递服务器中的mongodb连接将如何工作? Will it be a single connection or split into 100 connections, one for each request? 它是单个连接还是分成100个连接,每个请求一个?

How can I close the connection object in mongodb efficiently after the operation? 如何在操作后有效关闭mongodb中的连接对象? Or can we leave the connection in the express server as in the below code? 或者我们可以将连接保留在快速服务器中,如下面的代码所示?

Here follows my code.. 以下是我的代码..

Server.js 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 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? 1.快递服务器中的mongodb连接如何工作?

Once a connection is created to the mongodb database.(using mongoose or any other framework) It will create a pool of connections with that. 一旦建立到mongodb数据库的连接。(使用mongoose或任何其他框架)它将创建一个连接池。 (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. (Mongoose默认池大小为5,python中为100)创建的连接池由驱动程序维护,因此在需要与数据库的连接时可以重新使用这些连接。

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. 使用mongoose模型连接到数据库时,将从创建的连接池中分配单独的连接。

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 ? 2.如何在操作后有效关闭mongodb中的连接对象?

I am not sure 100% about this answer. 我不确定100%这个答案。 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 https://dzone.com/articles/deep-dive-connection-pooling

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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