Skip to content

Instantly share code, notes, and snippets.

@maxcnunes
Created May 28, 2014 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxcnunes/fbb8b224c2a6c653f7d9 to your computer and use it in GitHub Desktop.
Save maxcnunes/fbb8b224c2a6c653f7d9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
var MongoClient = require('mongodb').MongoClient,
format = require('util').format,
async = require('async');
MongoClient.connect('mongodb://127.0.0.1:27017/test_mongo', function(err, db) {
if(err) throw err;
var collection = db.collection('test_insert');
var startTime = new Date();
collection.remove({}, function(err) {
if (err) return console.log('remove err: ', err);
console.log('removed all');
async.timesSeries(1000000, insert, function(err) {
if (err) return console.log('end err: ', err);
count();
});
});
function insert (n, next) {
collection.insert({ my_index: n }, function(err) {
if (err) return next('insert err: ', err);
console.log('insert [%s] completed', n);
next();
});
}
function count () {
collection.count(function(err, count) {
if (err) return console.log('count err: ', err);
console.log(format("===> count = %s", count));
var endTime = new Date();
console.log('start [%s]\nend [%s]', startTime, endTime);
db.close();
});
}
});
@maxcnunes
Copy link
Author

mongo localhost:27017/test_mongo --quiet --eval "printjson(db.test_insert.count())"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment