Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

May 152013

Generate random data in MongoDB


You can use the following method to generate random string into a collection in MongoDB:

function randomString() { 
        var chars = 
        var randomstring = ''; 
        var string_length = 100;
        for (var i=0; i<string_length; i++) { 
                var rnum = Math.floor(Math.random() * chars.length); 
                randomstring += chars.substring(rnum,rnum+1); 
        return randomstring; 

for(var i=0; i<2000000; i++){{x:i, data:randomString()});}