Saving a million documents in Mongo using Nodejs and mongodb module

/**
* Created by davidsaliba on 13/03/2017.
*/
var MongoClient = require('mongodb').MongoClient
, format = require('util').format;

var url = 'mongodb://localhost:27017/test';
var async = require ('async');

var entry = {
data : "skdlfjsdf",
array : [ {id:"arr_obj1"} , {id:"arr_obj2"} ]
};

var entries = [];

var total_entries = 1000000

for (var j = 0 ; j <= total_entries ; j ++){
console.log (j);
entries.push({"_id":j ,entry:entry});
}

console.log ("Number of entries", entries.length);

MongoClient.connect(url, function(err, db) {
// Get the collection
var col = db.collection('articles');
//

var bulk = col.initializeOrderedBulkOp();
var counter = 0;

async.whilst(
// Iterator condition
function() { return counter < total_entries },

// Do this in the iterator
function(callback) {
counter++;

bulk.insert(entries[counter] );

if ( counter % 1000 == 0 ) {
bulk.execute(function(err,result) {
bulk = col.initializeOrderedBulkOp();
callback(err);
});
} else {
callback();
}
},

// When all is done
function(err) {
if ( counter % 1000 != 0 )
bulk.execute(function(err,result) {
console.log( "inserted some more" );
});
console.log( "I'm finished now" );
db.close();
}
);
});

nJoy 😉

Fix node Geoip update fails

After a routine update of a customer’s application resulted in a broken package

npm update

gyp ERR! configure error
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack at ChildProcess.onCpExit (/usr/local/lib/node_modules/npm/nod
gyp ERR! stack at ChildProcess.EventEmitter.emit (events.js:98:17)
gyp ERR! stack at Process.ChildProcess._handle.onexit (child_process.js:789:
gyp ERR! System Linux 2.6.32-431.1.2.0.1.el6.i686
gyp ERR! command “node” “/usr/local/lib/node_modules/npm/node_modules/node-gyp/b
gyp ERR! cwd /root/pmt/node_modules/geoip
gyp ERR! node -v v0.10.24
gyp ERR! node-gyp -v v0.12.1
gyp ERR! not ok
npm ERR! geoip@0.4.12 install: `node-gyp rebuild`
npm ERR! Exit status 1

 npm ls in the folder reveals  :  UNMET DEPENDENCY geoip 0.4.x  marked in red.

npm ERR! missing: geoip@0.4.x, required by Autheticatagainstdb@
npm ERR! not ok code 0

Solution :

(more…)