python3 获取mongodb表的索引

说明:此处脚本考虑到mongodb里面数据存储的时区转换,mongodb里面的数据使用的是 "格林威治"时间

 1 #!/usr/bin/python3
 2 
 3 import pytz
 4 from pymongo import MongoClient
 5 
 6 """
 7 /usr/bin/pip3 install -Iv pytz -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com;
 8 /usr/bin/pip3 install -Iv pymongo -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com;
 9 """
10 
11 tzinfo = pytz.timezone('Asia/Shanghai')
12 client = MongoClient(
13     host="x.x.x.x",
14     port=27017,
15     tz_aware=True,
16     tzinfo=tzinfo
17 )
18 
19 for database in client.list_database_names():
20     if database == 'admin' or database == 'config' or database == 'local':
21         continue
22     else:
23         db = client[database]
24         for doc in db.list_collection_names():
25             collection = db[doc]
26             print("{0}.{1} index: {2}".format(database, doc, collection.index_information()))

 

posted @ 2023-05-16 15:00  shadown404  阅读(43)  评论(0编辑  收藏  举报