Skip to content

Instantly share code, notes, and snippets.

@Losses
Created June 26, 2018 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Losses/90e4e4065940963de722b00f94619a60 to your computer and use it in GitHub Desktop.
Save Losses/90e4e4065940963de722b00f94619a60 to your computer and use it in GitHub Desktop.
import pymongo
from bson.json_util import dumps, RELAXED_JSON_OPTIONS
HOST = # Something like 'mongodb://localhost:23333'
DB = # What's the database of interest?
COLLECTION = # And the collection?
QUERY = # The query parameters, very similar to mongo in js, check out the ref below.
OUTPUT = # The json file will be written into which place?
# That's all you need to fill above.
__IS_FIRST__ = True
__COUNT__ = 0
out_file = open(OUTPUT, 'a+')
client = pymongo.MongoClient(HOST)
print('i Initializing...')
out_file.write('[')
print('i Querying...')
cursors = client[DB][COLLECTION].find(QUERY)
for cursor in cursors:
__COUNT__ += 1
print('i Writing result, ' + str(__COUNT__) + '...')
if not __IS_FIRST__:
out_file.write(',')
out_file.write(dumps(cursor, json_options = RELAXED_JSON_OPTIONS))
__IS_FIRST__ = False
out_file.write(']')
print('i Done.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment