Summary

Shortcuts

Supported methods

Only find(), aggregate() and update() are supported

Create a DB from BSON documents

It is possible to create a collection from an array of BSON documents with the bson mode. If no collection name is specified, documents will be inserted in a collection named collection, for example

[
    {
      "_id": 1, 
      "k": "value"
    },
    {
      "_id": 2, 
      "k": "someOtherValue"
    }
  ]

It is possible to create multiple collections in bson mode with custom names:

db={
    "coll1": [
      {
        "_id": 1, 
        "k": "value"
      }, 
      {
        "_id": 2, 
        "k": "someOtherValue"
      }
    ], 
    "coll2": [
      {
        "_id": 1, 
        "k2": "value"
      }
    ]
  }

This will create two collections named coll1 and coll2

Create a DB with mgodatagen

You can create random documents using mgodatagen. Select mgodatagen mode and create a custom configuration file.

The config file is an array of JSON documents, where each documents holds the configuration for a collection to create.

[
    // first collection to create 
    {  
     "collection":   <string>,          // required, collection name
     "count":        <int>,             // required, number of document to insert in the collection 
     "content": {                       // required, the actual schema to generate documents   
       "fieldName1": <generator>,       // optional, see Generator below
       "fieldName2": <generator>,
       ...
     }
    },
    // second collection to create 
    {
      ...
    }
  ]

Generator types

Generators have a common structure:

"fieldName": {                  // required, field name in generated document
  "type":             <string>, // required, type of the field
  "typeParam":        ...,      // specific parameters for this type

  "maxDistinctValue": <int>,    // optional, maximum number of distinct values for this field
  "nullPercentage":   <int>     // optional, int between 0 and 100. Percentage of documents
                                // that will have this field
}

List of <generator> types:

String

Generates a random string of a certain length. String is composed of char within this list: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_

"fieldName": {
    "type":             "string", // required
    "minLength":        <int>,    // optional, must be >= 0
    "maxLength":        <int>,    // optional, must be >= minLength
    "unique":           <bool>,   // optional, see details below
    "nullPercentage":   <int>,    // optional
    "maxDistinctValue": <int>     // optional
}

Unique String

If unique is set to true, the field will only contains unique strings. Unique strings have a fixed length, minLength is taken as length for the string. There is 64^x possible unique string for strings of length x. This number has to be inferior or equal to the number of documents you want to generate. For example, if you want unique strings of length 3, there is 64 * 64 * 64 = 262144 possible strings

They will look like

"aaa",
"aab",
"aac",
"aad",
...

Int

Generates a random int within bounds.

"fieldName": {
    "type":             "int", // required
    "min":              <int>, // optional
    "max":              <int>, // optional, must be >= min
    "nullPercentage":   <int>, // optional
    "maxDistinctValue": <int>  // optional
}

Long

Generates a random long within bounds.

"fieldName": {
    "type":             "long", // required
    "min":              <long>, // optional
    "max":              <long>, // optional, must be >= min
    "nullPercentage":   <int>,  // optional
    "maxDistinctValue": <int>   // optional
}

Double

Generates a random double within bounds.

"fieldName": {
    "type":             "double", // required
    "min":              <double>, // optional
    "max":              <double>, // optional, must be >= min
    "nullPercentage":   <int>,    // optional
    "maxDistinctValue": <int>     // optional
}

Decimal

Generates a random decimal128.

"fieldName": {
    "type":             "decimal", // required
    "nullPercentage":   <int>,     // optional
    "maxDistinctValue": <int>,     // optional
}

Boolean

Generates a random boolean.

"fieldName": {
    "type":             "boolean", // required
    "nullPercentage":   <int>,     // optional
    "maxDistinctValue": <int>      // optional
}

ObjectId

Generates a random objectId.

"fieldName": {
    "type":             "objectId", // required
    "nullPercentage":   <int>,      // optional
    "maxDistinctValue": <int>       // optional
}

Array

Generates a random array of bson object.

"fieldName": {
    "type":             "array",     // required
    "arrayContent":     <generator>, // required, generator use to create element
                                     // to fill the array. Can be of any type
    "minLength":        <int>,       // optional, must be >= 0
    "maxLength":        <int>,       // optional, must be >= minLength
    "nullPercentage":   <int>,       // optional
    "maxDistinctValue": <int>        // optional
}

Object

Generates random nested object.

"fieldName": {
    "type":                "object",    // required
    "objectContent": {                  // required, list of generator used to
       "nestedFieldName1": <generator>, // generate the nested document
       "nestedFieldName2": <generator>,
       ...
    },
    "nullPercentage":      <int>,       // optional
    "maxDistinctValue":    <int>        // optional
}

Binary

Generates random binary data of length within bounds.

"fieldName": {
    "type":             "binary", // required
    "minLength":        <int>,    // optional, must be >= 0
    "maxLength":        <int>,    // optional, must be >= minLength
    "nullPercentage":   <int>,    // optional
    "maxDistinctValue": <int>     // optional
}

Date

Generates a random date (stored as ISODate ).

startDate and endDate are string representation of a Date following RFC3339:

format: "yyyy-MM-ddThh:mm:ss+00:00"

"fieldName": {
    "type":             "date",   // required
    "startDate":        <string>, // required
    "endDate":          <string>, // required, must be >= startDate
    "nullPercentage":   <int>,    // optional
    "maxDistinctValue": <int>     // optional
}

Coordinates

Generates a random GeoJSON coordinates ( a GPS position in WSG84 decimal degrees with folowing format: [ longitude, latitude ]

eg : [40.741895, -73.989308]

"fieldName": {
    "type":             "coordinates", // required
    "nullPercentage":   <int>,         // optional
    "maxDistinctValue": <int>          // optional
}

Constant

Add the same value to each document.

"fieldName": {
    "type":           "constant", // required
    "constVal":       <object>,   // required, can be of any type including object and array
                                  // eg: {"k": 1, "v": "val"}
    "nullPercentage": <int>       // optional
}

Autoincrement

Generates an autoincremented value (type <long> or <int>).

"fieldName": {
    "type":           "autoincrement", // required
    "autoType":       <string>,        // required, either "int" or "long"
    "start":          <int|long>,      // optional, start value
    "nullPercentage": <int>            // optional
}

Reference

Use the same list of values for fields in different collection.

"fieldName":{
    "type":             "reference", // required
    "id":               <int>,       // required, generator id used to link
                                     // field between collections
    "refContent":       <generator>, // required, generator to use to create the
                                     // list of values
    "nullPercentage":   <int>,       // optional
    "maxDistinctValue": <int>        // optional
}

generator in other collections:

"fieldName": {
    "type":             "ref", // required
    "id":               <int>, // required, same id as previous generator
    "nullPercentage":   <int>, // optional
    "maxDistinctValue": <int>  // optional
}

Enum

Pick an object from an array as value for the field. Currently, objects in the array have to be of the same type. By default, items are picked from the array in the order where they appear.

"fieldName": {
    "type":           "enum", // required
    "values": [               // required. Can't be empty. An array of object of
      <object>,               // any type, including object and array.
      <object>
      ...
    ],
    "randomOrder":    <bool>, // optional. If set to true, objects will be picked 
                              // from the array in random order.
    "nullPercentage": <int>   // optional

}

UUID

Generate a random UUID ( using satori/go.uuid NewV4()).

"fieldName": {
    "type":           "uuid",   // required
    "format":         <string>, // optional, either "string" or "binary".
                                // default is "string"
    "nullPercentage": <int>     // optional
}

If format is "string", the field will be a simple string like "f1b9b567-9b34-45af-9d9c-35f565d57716".

If format is "binary", the field will be stored as a bson UUID (see bsonspec.org/spec.html) like BinData(4,"8bm1Z5s0Ra+dnDX1ZdV3Fg==").

StringFromParts

Generate a random string from several generators

"fieldName": {
    "type":           "stringFromParts", // required
    "parts": [                           // required. Can't be empty. An array 
      <generator>,                       // of generators of any basic type
      <generator>
      ...
    ],
    "nullPercentage": <int>              // optional
}

Example:

To generate phone number like '(555) 565-2431', you can combine several generators like this:

"phone": {
  "type": "stringFromParts",
  "parts": [
    {
      "type": "constant",
      "constVal": "(555) "
    },
    {
      "type": "int",
      "min": 100,
      "max": 999
    },
    {
      "type": "constant",
      "constVal": "-"
    },
    {
      "type": "int",
      "min": 1000,
      "max": 9999
    },
  ]
}

CountAggregator

Count documents from <database>.<collection> matching a specific query. To use a variable of the document in the query, prefix it with "$$".

The query can't be empty or null.

"fieldName": {
  "type": "countAggregator", // required
  "database":   <string>,    // required, db to use to perform aggregation
  "collection": <string>,    // required, collection to use to perform aggregation
  "query":      <object>     // required, query that selects which documents to count in
                             // the collection
}

Example:

Assuming that the collection first contains:

{"_id": 1, "field1": 1, "field2": "a" }
{"_id": 2, "field1": 1, "field2": "b" }
{"_id": 3, "field1": 2, "field2": "c" }

and that the generator for collection second is:

{
  "database": "test",
  "collection": "second",
  "count": 2,
  "content": {
    "_id": {
      "type": "autoincrement",
      "autoType": "int"
      "startInt": 0
    },
    "count": {
      "type": "countAggregator",
      "database": "test",
      "collection": "first",
      "query": {
        "field1": "$$_id"
      }
    }
  }
}

The collection second will contain:

{"_id": 1, "count": 2}
{"_id": 2, "count": 1}

ValueAggregator

Get distinct values for a specific field for documents from <database>.<collection> matching a specific query. To use a variable of the document in the query, prefix it with "$$".

The query can't be empty or null.

"fieldName": {
  "type": "valueAggregator", // required
  "database":   <string>,    // required, db to use to perform aggregation
  "collection": <string>,    // required, collection to use to perform aggregation
  "key":        <string>,    // required, the field for which to return distinct values.
  "query":      <object>     // required, query that specifies the documents from which
                             // to retrieve the distinct values
}

Example:

Assuming that the collection first contains:

{"_id": 1, "field1": 1, "field2": "a" }
{"_id": 2, "field1": 1, "field2": "b" }
{"_id": 3, "field1": 2, "field2": "c" }

and that the generator for collection second is:

{
  "database": "test",
  "collection": "second",
  "count": 2,
  "content": {
    "_id": {
      "type": "autoincrement",
      "autoType": "int"
      "startInt": 0
    },
    "count": {
      "type": "valueAggregator",
      "database": "test",
      "collection": "first",
      "key": "field2",
      "values": {
        "field1": "$$_id"
      }
    }
  }
}

The collection second will contain:

{"_id": 1, "values": ["a", "b"]}
{"_id": 2, "values": ["c"]}

BoundAggregator

Get the lowest and highest value for a specific field of documents in <database>.<collection> matching a specific query. To use a variable of the document in the query, prefix it with "$$"

The query can't be empty or null

"fieldName": {
  "type": "valueAggregator", // required
  "database":   <string>,    // required, db to use to perform aggregation
  "collection": <string>,    // required, collection to use to perform aggregation
  "key":        <string>,    // required, the field for which to return distinct values.
  "query":      <object>     // required, query that specifies the documents from which
                             // to retrieve lower/higer value
}

Example:

Assuming that the collection first contains:

{"_id": 1, "field1": 1, "field2": "0" }
{"_id": 2, "field1": 1, "field2": "10" }
{"_id": 3, "field1": 2, "field2": "20" }
{"_id": 4, "field1": 2, "field2": "30" }
{"_id": 5, "field1": 2, "field2": "15" }
{"_id": 6, "field1": 2, "field2": "200" }

and that the generator for collection second is:

{
  "database": "test",
  "collection": "second",
  "count": 2,
  "content": {
    "_id": {
      "type": "autoincrement",
      "autoType": "int"
      "startInt": 0
    },
    "count": {
      "type": "valueAggregator",
      "database": "test",
      "collection": "first",
      "key": "field2",
      "values": {
        "field1": "$$_id"
      }
    }
  }
}

The collection second will contain:

{"_id": 1, "values": {"m": 0, "M": 10}}
{"_id": 2, "values": {"m": 15, "M": 200}}

where m is the min value, and M the max value.

Faker

Generate 'real' data using gofakeit library.

"fieldName": {
    "type":             "faker",  // required
    "method":           <string>, // required, faker method to use, for example: Name
    "nullPercentage":   <int>,    // optional
    "maxDistinctValue": <int>     // optional
}

List of faker methods:

"FirstName"
"LastName"
"Name"
"NamePrefix"
"NameSuffix"
"Gender"
"Phone"
"PhoneFormatted"
"Username"
"Email"

"BS"
"BuzzWord"
"Company"
"CompanySuffix"
"JobDescriptor"
"JobLevel"
"JobTitle"
"Language"
"LanguageAbbreviation"

"CreditCardCvv"
"CreditCardExp"
"CreditCardType"
"CurrencyLong"
"CurrencyShort"

"DomainName"
"DomainSuffix"
"HTTPMethod"
"IPv4Address"
"IPv6Address"
"MacAddress"
"FileMimeType"
"SSN"
"URL"
"UserAgent"
"SafariUserAgent"
"OperaUserAgent"
"ChromeUserAgent"
"FileExtension"
"FirefoxUserAgent"

"Word"
"Question"
"Quote"
"Letter"
"ProgrammingLanguage"
"ProgrammingLanguageBest"
"HexColor"
"Color"
"HipsterWord"
"SafeColor"
"Street"
"StreetName"
"StreetNumber"
"StreetPrefix"
"StreetSuffix"
"City"
"State"
"StateAbr"
"Zip"
"Country"
"CountryAbr"

"TimeZone"
"TimeZoneAbv"
"TimeZoneFull"
"Month"
"WeekDay"

"Emoji"
"EmojiAlias"
"EmojiCategory"
"EmojiDescription"
"EmojiTag"

"HackerAbbreviation"
"HackerAdjective"
"HackeringVerb"
"HackerNoun"
"HackerPhrase"
"HackerVerb"

"CarMaker"
"CarModel"
"CarTransmissionType"
"CarFuelType"
"CarType"

"Animal"
"AnimalType"
"Cat"
"Dog"
"FarmAnimal"
"PetName"

"BeerAlcohol"
"BeerBlg"
"BeerHop"
"BeerIbu"
"BeerMalt"
"BeerName"
"BeerStyle"
"BeerYeast"

Limitations

Size limitations

This playground has several limitations:

Queries

Currently, the playground can run only find(), aggregate() and update() queries. Options in aggregation queries are not supported.

Shell regex

Currently, shell regex doesn't work in query.

so instead of

db.collection.find({
    "k": /pattern/
  })

use

db.collection.find({
    "k": {
      "$regex": "pattern"
    }
  })