我有一个非常简单的 mongodb 数据库,其中包含一个名为“test”的集合,我需要向集合中插入 133.306 条记录。这些记录存储在 JSON 文件中。这个文件的大小是 21Mb。50.000 条记录在一秒钟内成功插入。70.000 条记录插入挂起脚本。
编码:
var path = require('path'),
fs = require('fs'),
mongodb = require('mongodb'),
safe = { safe : true },
rowset;
rowset = JSON.parse(fs.readFileSync(path.join(__dirname, 'test.js')));
console.log('Total records: ' + rowset.length);
rowset = rowset.slice(0, 50000); // OK
// rowset = rowset.slice(0, 70000); // FAIL
console.log('Inserting ' + rowset.length + ' records');
mongodb.MongoClient.connect('mongodb://127.0.0.1:27017/browser',
function (err, client) {
if (err) throw err;
client.createCollection('test', safe, function (err, collection) {
if (err) throw err;
collection.insert(rowset, safe, function (err) {
if (err) throw err;
client.close(function (err) {
if (err) throw err;
console.log('done');
});
});
});
});
mongod 输出的最后几行:
Wed Dec 26 16:50:46 [initandlisten] connection accepted from 127.0.0.1:52003 #854 (4 connections now open)
Wed Dec 26 16:50:46 [initandlisten] connection accepted from 127.0.0.1:52004 #855 (5 connections now open)
Wed Dec 26 16:50:46 [initandlisten] connection accepted from 127.0.0.1:52005 #856 (6 connections now open)
这是行集中的典型记录:
{ _id: 133306,
product: 23089,
version: '1.0.0',
update: null,
edition: null,
lang: null,
entries: [ 54344, 54345 ] }
也许脚本达到了某些阈值或限制?