Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SHELL

import csv to MongoDB

import pandas as pd
from pymongo import MongoClient
import json

def mongoimport(csv_path, db_name, coll_name, db_url='localhost', db_port=27000)
    """ Imports a csv file at path csv_name to a mongo colection
    returns: count of the documants in the new collection
    """
    client = MongoClient(db_url, db_port)
    db = client[db_name]
    coll = db[coll_name]
    data = pd.read_csv(csv_path)
    payload = json.loads(data.to_json(orient='records'))
    coll.remove()
    coll.insert(payload)
    return coll.count()
Source by gist.github.com #
 
PREVIOUS NEXT
Tagged: #import #csv #MongoDB
ADD COMMENT
Topic
Name
1+4 =