var / log / mongodb / mongod.log从node.js打开和closures连接的无限循环

/var/log/mongodb/mongod.log里面我有一个无限循环的打开和closures连接。 这是使mongod.log巨大,并占用磁盘空间。 我也担心我的node.js应用程序中有错误的代码。 我在我的节点js应用程序中使用mongoose和本地mongo驱动程序。 我没有看到任何错误或例外。

有没有人看过这个,可能是什么原因? 日志现在是11GB,并不断增长。

 2017-07-14T17:51:59.562+0000 I - [conn21293746] end connection 127.0.0.1:49373 (19 connections now open) 2017-07-14T17:51:59.638+0000 I NETWORK [thread1] connection accepted from 127.0.0.1:49374 #21293747 (19 connections now open) 2017-07-14T17:51:59.639+0000 I NETWORK [conn21293747] received client metadata from 127.0.0.1:49374 conn21293747: { driver: { name: "nodejs", version: "2.2.28" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "3.13.0-117-generic" }, platform: "Node.js v6.10.2, LE, mongodb-core: 2.1.12" } 2017-07-14T17:51:59.641+0000 I - [conn21293747] end connection 127.0.0.1:49374 (19 connections now open) 2017-07-14T17:52:00.008+0000 I NETWORK [thread1] connection accepted from 127.0.0.1:49375 #21293748 (19 connections now open) 2017-07-14T17:52:00.008+0000 I NETWORK [conn21293748] received client metadata from 127.0.0.1:49375 conn21293748: { driver: { name: "nodejs", version: "2.2.28" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "3.13.0-117-generic" }, platform: "Node.js v6.10.2, LE, mongodb-core: 2.1.12" } 2017-07-14T17:52:00.013+0000 I - [conn21293748] end connection 127.0.0.1:49375 (19 connections now open) 2017-07-14T17:52:00.547+0000 I NETWORK [thread1] connection accepted from 127.0.0.1:49376 #21293749 (19 connections now open) 2017-07-14T17:52:00.548+0000 I NETWORK [conn21293749] received client metadata from 127.0.0.1:49376 conn21293749: { driver: { name: "nodejs", version: "2.2.28" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "3.13.0-117-generic" }, platform: "Node.js v6.10.2, LE, mongodb-core: 2.1.12" } 2017-07-14T17:52:00.550+0000 I - [conn21293749] end connection 127.0.0.1:49376 (19 connections now open) 

一些经常被调用的特定的node.js代码

 var MongoClient = require('mongodb').MongoClient; exports.logSession = function(payload) { MongoClient.connect(url, function (err, db) { assert.equal(null, err); insertSessionIntoTable(payload, db, function () { db.close(); }) }) }; function insertSessionIntoTable(payload, db, callback) { db.collection(collectionName).insertOne(payload, function (err, result) { assert.equal(err, null); logger.verbose("Inserted a document into the collection."); callback(); }); 

你的代码将打开每个会话的连接

 exports.logSession = function(payload) { MongoClient.connect(url, function (err, db) { // for every session your code will open new connection assert.equal(null, err); insertSessionIntoTable(payload, db, function () { db.close(); // here it will close it }) }) }; 

打开连接一次,并使用相同的连接做所有的操作。

 var db; MongoClient.connect(url) .then(function (dbInstance) { // <- db as first argument db = dbInstance; }) .catch(function (err) {}) exports.logSession = function(payload) { insertSessionIntoTable(payload, db, function () { // work done }) }) };