Skip to content

Instantly share code, notes, and snippets.

@maxired
Created May 22, 2012 09:35
Show Gist options
  • Save maxired/2767861 to your computer and use it in GitHub Desktop.
Save maxired/2767861 to your computer and use it in GitHub Desktop.
A function used to split Mongo Document before an insertion
This Gist show a way to split an array of mongo docuement before inserting them if we thinks this array my be to big for be inserted.
the _insertData function is a private function which insert the data the data contained in options.data in the collection `collection` and then call the `callback`
function _bulkInsertData(options, collection, endCallback ) {
var sended = [];
var errors = [];
var pushed = function(err, pushedData){
if(err){
errors.push(err);
}
if(pushedData!=null){
sended = sended.concat(pushedData);
}
}
var queue = async.queue(function(datas, callback) {
var data= datas.data;
if(bson.BSON.calculateObjectSize(data)> maxBsonSize ) {
if(data.length<2){
//return callback(new Error("The document is too big to be inserted"));
return callback( new Error("The document is too big to be inserted"));
}
queue.push({data : data.slice(0, data.length/2)}, pushed);
queue.push({data : data.slice(data.length/2, data.length)}, pushed);
callback(null,null);
}else{
var cloneOptions = {};
Object.keys(options).forEach(function(key){
if(key!=="data"){
cloneOptions[key]=options[key];}
});
cloneOptions.data = data;
_insertData(cloneOptions, collection, callback);
};
},2);
queue.drain = function(){
options.data = sended;
endCallback( errors.length>0? errors : null, options);
};
queue.push({data : options.data}, pushed)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment