Load data in a json file into mongo db

“””
Load data in a json file into mongo db
“””
import os
import json
from pymongo import MongoClient

CONN_STR = ‘mongodb://{}’
CONN_STR_AUTH = ‘mongodb://{}:{}@{}’

DB_NAME = “”
MONGO_SERVER = “127.0.0.1:27017”

conn_str = CONN_STR.format(MONGO_SERVER)
conn = MongoClient(conn_str)

db = conn[DB_NAME]

def get_collection_name_by_file(file_path):
“””
“””
collection_name = os.path.splitext(os.path.basename(file_path))[0]
return collection_name.replace(“-“, “_”)
def load_array_to_db(data, clear=False):
“””
“””
collection_name = get_collection_name_by_file(json_path)
collection = db[collection_name]
if clear:
# db.drop_collection(collection_name)
collection.remove({})

for item in data:
collection.insert(item)
def load_json_file_to_db(json_path, clear=False):
“””
“””
try:
with open(json_path) as data_file:
data = json.load(data_file)
load_array_to_db(data, clear)
except Exception as e:
print “load json file failed with error : {0}”.format(e.message)

print “Finished…”
if __name__ == ‘__main__’:
base_dir = os.path.dirname(os.path.realpath(__file__))
json_path = os.path.join(base_dir, “follower.json”)
load_json_file_to_db(json_path, clear=True)

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a comment