Skip to content

Instantly share code, notes, and snippets.

@claustres
Created August 28, 2017 08:52
Show Gist options
  • Save claustres/4ca85f06dad0af6674602fc54d3d6e17 to your computer and use it in GitHub Desktop.
Save claustres/4ca85f06dad0af6674602fc54d3d6e17 to your computer and use it in GitHub Desktop.
Create MongoDB collection
db = db.getSiblingDB('random');
var minDate = new Date(2012, 0, 1, 0, 0, 0, 0);
var maxDate = new Date(2013, 0, 1, 0, 0, 0, 0);
var delta = maxDate.getTime() - minDate.getTime();
var documentNumber = arg1;
var batchNumber = 1000;
var start = new Date();
var batch = db.randomData.initializeUnorderedBulkOp();
var batchCount = 0;
var index = 1;
while (index <= documentNumber) {
var date = new Date(minDate.getTime() + Math.random() * delta);
var value = 100 * Math.random();
var document = {
timestamp : date,
value : value
};
batch.insert(document);
batchCount++;
// Insert batch
if (batchCount === batchNumber) {
printjson(batch.execute());
print('Inserted ' + db.randomData.count() + ' documents.');
batch = db.randomData.initializeUnorderedBulkOp();
batchCount = 0;
}
index++;
}
// Insert last batch
if ( batchCount > 0 ) {
printjson(batch.execute());
print('Inserted ' + db.randomData.count() + ' documents.');
}
print('Saved ' + db.randomData.count() + ' in ' + (new Date() - start)/1000.0 + 's');
start = new Date();
printjson(db.randomData.createIndex({value:1, timestamp:1}));
print('Created indices in ' + (new Date() - start)/1000.0 + 's');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment