python mongodump

Solutions on MaxInterview for python mongodump by the best coders in the world

showing results for - "python mongodump"
Marwane
20 Nov 2016
1from os.path import join
2import pymongo
3from bson.json_util import dumps
4
5def backup_db(backup_db_dir):
6    client = pymongo.MongoClient(host=..., port=..., username=..., password=...)
7    database = client[<db_name>]
8    collections = database.collection_names()
9
10    for i, collection_name in enumerate(collections):
11        col = getattr(database,collections[i])
12        collection = col.find()
13        jsonpath = collection_name + ".json"
14        jsonpath = join(backup_db_dir, jsonpath)
15        with open(jsonpath, 'wb') as jsonfile:
16            jsonfile.write(dumps(collection).encode())
17
18
19backup_db('.')
20
similar questions
queries leading to this page
python mongodump