同步Mongo插入是否有限制?
我有一组代码,我从CSV填充。 总共有150万码,不less。 CSV很容易被parsing成一个对象或forms:
codes = [ {code:'abc'}, {code:'123'}, etc ]
我最初试图写这个Mongo在一个插入,像这样
Code.collection.insert(codes)
(使用Mongoose直接编写查询)。
然而,这失败了默默。 假设某种隐藏的内存问题,我开始分块我的代码,发现Mongo 2.6(本地运行在我的16Gb Macbook,没有副本集)将接受大约1000代码在一个插入。
这是预期的行为,如果是这样,这个数字有什么理由吗?
尝试使用批量操作方法进行插入,特别是需要使用Model.collection
访问器对象可以在Mongoose中公开的db.collection.initializeOrderedBulkOp()
方法。 因此,在上面你可以重组你的插入来做批量更新,如下所示:
var bulk = Code.collection.initializeOrderedBulkOp(), counter = 0; codes.forEach( function(obj) ) { bulk.find({'code': {'$ne': null}}/* some search */) .update({'$set': {'code': obj.code}}); counter++; if (counter % 1000 == 0) { bulk.execute(function(err, result) { bulk = Code.collection.initializeOrderedBulkOp(); }); } } if (counter % 1000 != 0 ) { bulk.execute(function(err, result) { // get stats }); }