vgrabovets
2/21/2017 - 2:10 PM

get data from mongoDB

get data from mongoDB

import pymongo
import time

def get_data_from_mongo(ip, db_name, collection, print_freq, *arg):
    t0 = time.time()
    client = pymongo.MongoClient(ip)
    db = client[db_name].get_collection(collection)
    res = db.find(*arg)
    temp_dict = dict()
    cnt = 0
    for r in res:
        temp_dict[r['_id']] = r
        cnt += 1
        if cnt % print_freq == 0:
            print('\rRead: %d, elapsed time: %0.2f mins' % (cnt, round((time.time() - t0) / 60, 2)),
                  end='', flush=True)
    print('\nTotal read: %d, elapsed time: %0.2f mins' % (cnt, round((time.time() - t0) / 60, 2)))
    client.close()
    return temp_dict