1. 程式人生 > >python實踐——批量統計mongodb資料庫的集合大小

python實踐——批量統計mongodb資料庫的集合大小

#!/usr/bin/env python
import os,sys
list = []
for i in range(3,50):
    l = os.popen("/bin/echo 'show collections'| /usr/local/mongodb/mongodb/bin/mongo 127.0.0.1:27103/資料庫名|sed -n '%sp'" %i)      #獲取資料庫的集合列表
    l = l.read()
    if l != "":
        list.append(l)
    else:
        print "the collections is %s" %i
        del list[i-4
] break for i in list: i = i.strip("\n") os.popen("/bin/echo 'db.%s.stats()'| /usr/local/mongodb/mongodb/bin/mongo 127.0.0.1:27103/資料庫名|sed -n '4p;6p;8p'>>mongodb_size_count.txt" %i) #統計集合的大小並把結果寫入mongodb_size_count.txt檔案中