Skip to content

Instantly share code, notes, and snippets.

@mprajwala
Last active July 23, 2023 20:07
Show Gist options
  • Save mprajwala/849b5909f5b881c8ce6a to your computer and use it in GitHub Desktop.
Save mprajwala/849b5909f5b881c8ce6a to your computer and use it in GitHub Desktop.

Revisions

  1. mprajwala revised this gist May 20, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion import_csv_to_mongo
    Original file line number Diff line number Diff line change
    @@ -20,5 +20,5 @@ def import_content(filepath):
    db_cm.insert(data_json)

    if __name__ == "__main__":
    filepath = '/path/to/csv/path'
    filepath = '/path/to/csv/path' // pass csv file path
    import_content(filepath)
  2. mprajwala renamed this gist May 20, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. mprajwala created this gist May 20, 2014.
    24 changes: 24 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    #!/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'
    import_content(filepath)