【发布时间】:2015-04-02 23:42:54
【问题描述】:
我正在使用以下代码测试我的 nodejs 插入到我的 MongoDB 中。当我插入类似 10000 行的内容时,一切正常。但是,如果我尝试插入类似 100 万的内容,则插入操作会停止一段时间,并且不会在 nodejs 控制台或 MongoDB 上打印出错误。
我在下面附上了我的代码和控制台,请帮助我,非常感谢!!
--- 更新 ---
为了回答回复中的问题,我还检查了我的 mongostat,插入会在一段时间后停止。但是,我观察到两个现象:
1) 在我的笔记本电脑上,mongodb 解析器是通过“npm install mongodb”安装的,并且“node my.js”通过以下服务器输出“服务器已启动”启动。
观察结果:insert正在进行中,mongostat显示,大部分时间insert为零,但有时可以显示插入的记录。
2) 在我的 PC 上,通过“npm --registry http://registry.npmjs.eu/ install mongodb”安装 mongodb 解析器,并以以下服务器输出“服务器已启动。-> 失败”启动“node my.js”加载c++ bson扩展,使用纯JS版本"
观察结果:插入只运行了一段时间,然后什么也没有发生,一段时间后 mongostat 总是显示零插入。
“npm --registry http://registry.npmjs.eu/ install mongodb”也会有问题吗?
我的nodejs代码:
mongoClient.connect("mongodb://localhost:27017/testdb", { db : { native_parser : true } }, function(err, database) {
if (err) { console.log(err.message); throw err; }
// create new collection under database
var collection = database.collection('gm_std_measurements_coveringindex');
date = new Date();
// add all Documents
for (var i = 0; i < 1000000; i++) {
var ranNumber = Math.floor((Math.random() * 1000) + 1);
// insert objects after table and index creation
var istObject = {
fkDataSeriesId : ranNumber,
measDateUtc : date,
measDateSite : date,
project_id : ranNumber,
measvalue : ranNumber,
refMeas : false,
reliability : 1.0
};
collection.insert(istObject, { w : 1 }, function(err, docs) {
if (err) {
console.log(err.message);
throw err;
} else {
// do noting to responsed inserted Document
}
});
}
console.log("* Documents created!");
});
MongDB 服务器输出:
Thu Apr 17 15:32:18.942 [initandlisten] connection accepted from 127.0.0.1:33228 #70 (3 connections now open)
Thu Apr 17 15:32:18.949 [conn70] end connection 127.0.0.1:33228 (2 connections now open)
Thu Apr 17 15:32:18.951 [initandlisten] connection accepted from 127.0.0.1:33229 #71 (3 connections now open)
Thu Apr 17 15:32:18.952 [initandlisten] connection accepted from 127.0.0.1:33230 #72 (4 connections now open)
Thu Apr 17 15:32:18.952 [initandlisten] connection accepted from 127.0.0.1:33231 #73 (5 connections now open)
Thu Apr 17 15:32:18.953 [initandlisten] connection accepted from 127.0.0.1:33232 #74 (6 connections now open)
Thu Apr 17 15:32:18.953 [initandlisten] connection accepted from 127.0.0.1:33233 #75 (7 connections now open)
Thu Apr 17 15:32:28.520 [FileAllocator] allocating new datafile /var/lib/mongodb/testdb.2, filling with zeroes...
Thu Apr 17 15:32:28.563 [FileAllocator] done allocating datafile /var/lib/mongodb/testdb.2, size: 256MB, took 0.042 secs
Thu Apr 17 15:32:31.517 [conn75] insert testdb.gm_std_measurements_coveringindex ninserted:1 keyUpdates:0 locks(micros) w:23 129ms
Thu Apr 17 15:32:31.517 [conn72] insert testdb.gm_std_measurements_coveringindex ninserted:1 keyUpdates:0 locks(micros) w:37 129ms
Thu Apr 17 15:32:31.517 [conn74] insert testdb.gm_std_measurements_coveringindex ninserted:1 keyUpdates:0 locks(micros) w:31 129ms
Thu Apr 17 15:32:31.517 [conn73] insert testdb.gm_std_measurements_coveringindex ninserted:1 keyUpdates:0 locks(micros) w:19 129ms
【问题讨论】:
-
你对每个请求都创建一个连接,我认为连接数是有限的,这就是问题
-
感谢您的回复!我不太明白,我认为从“mongoClient.connect”获取的“数据库”是一个连接池,其中LOOP内部的“collection.insert”应该在内部重用这个连接池?如果我的理解是错误的,那么正确的应该是怎样的呢?