-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmongoDocsToJson.py
38 lines (28 loc) · 1.05 KB
/
mongoDocsToJson.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Utilitarian script to retrieve all the documents in a database + collection,
and dump them in a json file
NOTE: in order to get pymongo in the path, you need to source the reqmgr2ms init.sh script
source /data/srv/current/sw/slc7_amd64_gcc630/cms/reqmgr2ms/0.4.5.pre3/etc/profile.d/init.sh
"""
from __future__ import print_function, division
import json
import sys
import pymongo
def main():
myClient = pymongo.MongoClient("mongodb://localhost:8230/")
myDB = myClient["msOutDB"]
collections = ["msOutRelValColl", "msOutNonRelValColl"]
for coll in collections:
myCol = myDB[coll]
resp = myCol.find()
docs = list(resp)
print("Found {} documents in the DB collection: {}".format(len(docs), coll))
fileName = "/data/user/{}.json".format(coll)
print("saving summary in file: {}\n".format(fileName))
with open(fileName, "w") as fObj:
json.dump(docs, fObj, indent=2)
print("Done!")
if __name__ == '__main__':
sys.exit(main())