简体   繁体   English

使用NodeJS和Mongoose在MongoDB中打开了多个连接

[英]More than one connection opened in MongoDB using NodeJS and Mongoose

I am new to Node and MongoDB, so please forgive me if I sound too naive. 我是Node和MongoDB的新手,所以如果我听起来太天真,请原谅我。

I have a DB class that creates a connection to MongoDB 我有一个数据库类,用于创建与MongoDB的连接

db.service.js db.service.js

const mongoose = require("mongoose");
const fs = require("fs");
const dbConfigs = JSON.parse(fs.readFileSync("/configs.json"));
const dbHost = dbConfigs.DB_HOST;
const dbName = dbConfigs.DB_NAME;
const dbPort = dbConfigs.DB_PORT;
const dbUrl = "mongodb://" + dbHost + ":" + dbPort + "/" + dbName;
const dbOptions = {
    useNewUrlParser: true
};

let dbConnection = mongoose.createConnection(dbUrl, dbOptions);
exports.getConnection = () => {
    if (dbConnection)
        return dbConnection;
}

exports.closeConnection = () => {
    if (dbConnection)
        return dbConnection.close();
}

Next I have another module that creates a Schema for MongoDB 接下来,我还有另一个模块,该模块为MongoDB创建架构

schema.js schema.js

const connection = require("./db.service").getConnection();
const Schema = require("mongoose").Schema;

const SampleSchema = new Schema({...})

exports.Sample = connection.model("Sample", SampleSchema);

Then I have another module that makes use of this Schema to save objects 然后我有另一个模块,利用该模式保存对象

logger.js logger.js

const Sample = require("./schema").Sample;

exports.log = () => {

let body = {....};
let sampleObj = new Sample(body);
return sampleObj.save(sampleObj);

The Main module 主模块

Main.js Main.js

const logger = require("./logger");

for (let i=0; i < 100; i++) {
    logger.log();
}

When I run node Main.js , everything is saved, but when I check the MongoDB with the command db.serverStatus.connections , I see 6 connections open. 当我运行node Main.js ,所有内容都会保存,但是当我使用db.serverStatus.connections命令检查MongoDB时,我看到有6个连接打开。

I don't know how it's getting there. 我不知道怎么到那里。 I know the command mongo itself keep a connection open, but where are the other 5 connections coming from? 我知道mongo命令本身会使连接保持打开状态,但其他5个连接来自何处?

I have checked this , which seems to suggest that Mongoose opens 5 connections for an application, but my application is firing just one transaction, where is the need to open 5 connection then? 我已经检查过 ,这似乎表明Mongoose为一个应用程序打开了5个连接,但是我的应用程序仅触发了一个事务,那么哪里需要打开5个连接? Couldn't it be done with just one? 不能只用一个完成吗?

Mongoose is managing the connections itself; 猫鼬在管理连接本身。 it tries to optimize the speed of the request. 它尝试优化请求的速度。

You are seeing it like you are using only one transaction, but behind the scene mongoose is probably doing much more than you expect. 您会看到它就像在使用一次事务,但是在幕后猫鼬的作用可能超出您的预期。


You can modify the number of connection mongoose can open using the parameter poolSize . 您可以使用参数poolSize修改可以打开的猫鼬连接数。


Example : 范例

const dbOptions = {
    useNewUrlParser: true,
    poolSize: 1,
};

Try it with poolSize equals to 1 and see how much time it takes to execute your transaction, you'll have your answer. poolSize等于1的情况下尝试一下,看看执行事务需要花费多少时间,您将得到答案。

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

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