为什么我的nodejs插入到mongoDB一段时间后停止

我正在testing我的nodejs插入到我的MongoDB与下面的代码。 当我插入像10000行的东西,一切工作文件。 但是,如果我尝试插入一些像100万,插入操作停止一段时间没有错误打印出nodejs控制台也没有MongoDB。

我附上我的代码和控制台的MongoDB下面,请帮助我,非常感谢!

—更新—

回答问题的答案,我也检查了我的mongostat,插入后停止。 不过,我观察到两个现象:

1)在我的笔记本电脑上,通过“npm install mongodb”安装mongodbparsing器,并在服务器输出“Server has started”下启动“node my.js”。

观察结果:插入正在进行中,mongostat显示,大部分时间插入为零,但有时插入的logging可以显示。

2)在我的电脑上,通过“npm –registry http://registry.npmjs.eu/安装mongodb”来安装mongodbparsing器,并且“node my.js”以下面的服务器输出启动“Server has started – >无法加载c ++ bson扩展名,使用纯JS版本“

观察结果:插入仅运行一段时间,然后没有任何反应,并且mongostat在一段时间后总是显示零插入。

那么“npm –registry http://registry.npmjs.eu/安装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 

我想我已经弄明白了。

从Nodejs中插入数百万个数据到MongoDB的问题是nodejs被生成这些logging并立即发送出去所占用。 所有数以百万计的数据立即出现,并且nodejs本身在一段时间之后就会陷入困境,而此时MongoDB并没有收到太多的接收插入。

我通过设置一次插入10000个logging的间隔方法来“摆脱”这个问题,并且每秒都会调用这个间隔,这样nodejs就有机会移动到下一个事件中去发送生成的插入,然后启动再次插入的代。

 interval_checkId = setInterval(createSomeInsert, 1000); 

和createSomeInsert方法:

 function createSomeInsert() { console.log("* Starting to insert another 10K records, current count: " + counter); var collection = dbPool.collection('gm_std_measurements_coveringindex'); var date = new Date(); var amount = 10 * 1000; for(var i = 0; i < amount; 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 : 0 }, function(err, docs){ if(err) {console.log(err); throw err;} counter++; }); } if(counter >= totalTarget) { clearInterval(interval_checkId); console.log("Completed Insert, in total : " + counter); counter = 0; } } 

然而,我仍然不明白为什么nodejs会在生成很多插入请求之后被阻塞,并且为什么大部分插入请求不会在后台被发送到mongodb。

您可以使用mongostat来检查脚本的进度或查看是否有任何事情发生。

打开cmd,导航到/ mongodb / bin目录并执行mongostat –host localhost

这会给你提供有关你的mongodb服务器当前正在发生的事情。 第一列是插入操作,你必须检查。 这表示在db中每秒插入多less项目。 也看看“locking的数据库”列,它显示了数据库的写locking百分比。 较高的百分比意味着写入操作速度较慢,因为有很多插入操作,而且每个插入操作都在等待前一个完成并解锁数据库。 还要注意connection.insert是asynchronous操作,基本上你试图一次向数据库发送一百万个插入请求,因为你没有等待任何插入完成。 我10分钟前开始写剧本,目前在40万个项目