Created
June 26, 2018 06:22
-
-
Save Losses/90e4e4065940963de722b00f94619a60 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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