Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

How to generate random test data in MongoDB

farenda 2017-04-09 0

A common problem when starting with MongoDB is how to generate random test data in MongoDB. A large data set is especially needed for MongoDB performance tuning.

Function to generate random ISODate

This simple function returns an ISODate from within last 24 hours. You can extend it to return dates from other date ranges:

var day = 1000 * 60 * 60 * 24;
var randomDate = function () {
  return new Date(Date.now() - (Math.floor(Math.random() * day)));
}

When executed it returns random ISODate() as expected:

> new Date()
ISODate("2017-04-09T19:36:43.417Z")
> randomDate()
ISODate("2017-04-09T06:43:24.249Z")

Function to generate random name/string

Another helpful function, this time to generate, for example, names of users:

var randomName = function() {
  // Base 36 uses letters and digits to represent a number:
  return (Math.random()+1).toString(36).substring(2);
}

And here it’s results:

> randomName()
rj611nxjre

How to insert 1.000.000 random documents

Now we can use the following loop to insert pseudo-random documents into MongoDB:

for (var i = 1; i <= 1000000; ++i) {
  db.test.insert({
      name: randomName(),
      creationDate: randomDate(),
      uid: i
  });
}

On my laptop it took about 10 minutes to insert 1 million of random documents.

> for (var i = 1; i <= 1000000; ++i) {
    var randomName = (Math.random()+1).toString(36).substring(2);
    db.test.insert({name: randomName, creationDate: randomDate(), uid: i});
  }
... ... ... WriteResult({ "nInserted" : 1 })
> db.test.count()
1000000

A sample, generated MongoDB document looks like this:

{
        "_id" : ObjectId("58ea8976733313ff20cfc9e3"),
        "name" : "caed7zs2f6",
        "creationDate" : ISODate("2017-04-09T07:31:47.422Z"),
        "uid" : 1
}

Have fun! :-)

References:

  • Check out MongoDB Tutorial!
Share with the World!
Categories MongoDB Tags mongodb
Previous: Spring Locale from Request
Next: MongoDB JSON to Document/DBObject

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2021

sponsored
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok