Skip to content

How to spin new MongoDB instance on Amazon (AWS)

considerable edited this page Mar 6, 2019 · 6 revisions

MongoDB instance for AWS or MongoDB container for Docker

the easy way, it can be obtained from Bitnami - https://bitnami.com/stack/mongodb/helm

and then load your data sample (say, data/sample.json) with a script similar to the following:

#!/usr/bin/env python

"""
Read data/sample.json file, validate it, and load to MongoDB sample collection as BSON,
almost per http://api.mongodb.com/python/current/tutorial.html
"""

import json
import pymongo
from pymongo import MongoClient

def all_json_list(filename='data/sample.json'):
    try:
        with open(filename) as f:
            return f.readlines()
    except Exception:
        print('Oops, Exception...')
        exit(1)

client = MongoClient('mongodb://root:<secret>@127.0.0.1')
db = client['homeless']
collection = db['tweets']

print('Sampling the data...')

for each_row in all_json_list():
    try:
        valid_json_dict = json.loads(each_row)
        print collection.insert_one(valid_json_dict).inserted_id
    except ValueError:
        continue

print('Done loading collection to MongoDB')