/ MongoDB

Generate Random Data for MongoDb

a Python script to generate random data to MongoDb using the Faker library for Python.

I will include some of my scripts for Elasticsearch, DynamoDB and CSV at the bottom of this post.


$ pip install pymongo
$ pip install fake-factory
``` <p>

**Python Script to Generate Data:**

from pymongo import MongoClient
from faker import Factory
import time

client = MongoClient('mongodb://user:pass@host.domain.com:23325/db')
db = client.db

def create_names(fake):
    for x in range(10):
        genName = fake.first_name()
        genSurname = fake.last_name()
        genJob = fake.job()
        genCountry = fake.country()

        result = db.col1.insert_one(
                'name': genName,
                'surname': genSurname,
                'job': genJob,
                'country': genCountry

        print 'id: ' + str(result.inserted_id) + ' name: ' + genName

if __name__ == '__main__':
    fake = Factory.create()
``` <p>

Similar scripts to generate data to other services:

* [Elasticsearch](https://gitlab.com/rbekker87/scripts/blob/fafa02e58b4ad5d3f6fac281ac2bfceb8f2543d0/generate-random-data-elasticsearch.py)
* [DynamoDB](https://gitlab.com/rbekker87/scripts/blob/fafa02e58b4ad5d3f6fac281ac2bfceb8f2543d0/generate-random-data-dynamodb.py)
* [CSV](https://gitlab.com/rbekker87/scripts/blob/fafa02e58b4ad5d3f6fac281ac2bfceb8f2543d0/generate-csv-data.py)