-
Notifications
You must be signed in to change notification settings - Fork 7
/
dumpdb.py
executable file
·38 lines (30 loc) · 1.27 KB
/
dumpdb.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/python
"""
Dump the MongoDB
USAGE:
./dumpdb.py -c collectionname
"""
import sys, string
import common.mongodb
import common.json
from pprint import pprint
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-c", "--collection", dest="collection", help="collection name")
parser.add_option("-d", "--database", dest="database", help="database name")
parser.add_option("-p", "--port", dest="port", help="port number for mongodb", type="int")
parser.add_option("--hostname", dest="hostname", help="hostname for mongodb")
(options, args) = parser.parse_args()
if options.database is None:
print "Databases:", common.mongodb.connection(PORT=options.port, HOSTNAME=options.hostname).database_names()
sys.exit(0)
if options.collection is None:
print "Collections:", common.mongodb.db(DATABASE=options.database, PORT=options.port, HOSTNAME=options.hostname).collection_names()
sys.exit(0)
collection = common.mongodb.collection(DATABASE=options.database, name=options.collection, PORT=options.port, HOSTNAME=options.hostname)
for doc in common.mongodb.findall(collection, matchfn=lambda doc: True):
try:
common.json.dump(doc, sys.stdout, indent=4)
except:
pprint(doc)
# print string.strip(doc["content"].encode("utf-8"))