Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. import pymongo
  2. from pymongo import MongoClient
  3. import pandas as pd
  4.  
  5. def mongo_to_files(usr_name, pwd, address, db_name, collection,
  6. out_path='out{}-{}.json', chunk_size=10000, port=27017):
  7. client = MongoClient(f'mongodb://{usr_name}:{pwd}@{address}', port)
  8. db = client[db_name]
  9. collection = db[collection]
  10. total = collection.count()
  11. n = 0
  12. while n < total:
  13. data = list(collection.find().skip(n).limit(chunk_size))
  14. for row in data:
  15. row.pop('_id')
  16. filename = out_path.format(n, n+chunk_size)
  17. pd.DataFrame(data).to_json(filename, orient='records', force_ascii=False)
  18. n += len(data)
  19. return n
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement