Skip to content

Instantly share code, notes, and snippets.

@alpoza
Forked from mprajwala/import_csv_to_mongo
Created February 26, 2017 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alpoza/d8a2b098ac2a2e058af4b94ca5319225 to your computer and use it in GitHub Desktop.
Save alpoza/d8a2b098ac2a2e058af4b94ca5319225 to your computer and use it in GitHub Desktop.
Store CSV data into mongodb using python pandas
#!/usr/bin/env python
import sys
import pandas as pd
import pymongo
import json
def import_content(filepath):
mng_client = pymongo.MongoClient('localhost', 27017)
mng_db = mng_client['mongodb_name'] // Replace mongo db name
collection_name = 'collection_name' // Replace mongo db collection name
db_cm = mng_db[collection_name]
cdir = os.path.dirname(__file__)
file_res = os.path.join(cdir, filepath)
data = pd.read_csv(file_res)
data_json = json.loads(data.to_json(orient='records'))
db_cm.remove()
db_cm.insert(data_json)
if __name__ == "__main__":
filepath = '/path/to/csv/path' // pass csv file path
import_content(filepath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment