查看集合大小
show dbs;
[root@ip ~]# cat get_size.sh
#!/bin/sh
MONGO=/usr/bin/mongo
function get_dbs() {
$MONGO << EOF
show dbs
EOF
}
function get_tables() {
# $1 is dbname
DB=$1
$MONGO << EOF
use $DB ;
show tables;
EOF
}
function get_a_table_size() {
DB=$1
TABLE=$2
$MONGO << EOF
use $DB ;
db.$TABLE.totalSize()
EOF
}
function get_size() {
for i in `get_tables $1 |head -n -1 |tail -n +5`
do
SIZE=`get_a_table_size $1 $i |sed -n 5p`
echo "$SIZE $i"
done
}
echo $1
get_size $1 | tail -n +2 | sort -r -n | awk '{size=$1/1024;if(size<1024){printf("%10.3f KB\t%s\n",size,$2);}else{size=size/1024;if(size<1024){printf("\033[36m%10.3f MB\t%s\n\033[0m",size,$2);}else{size=size/1024;if(size<1024){printf("\033[35m%10.3f GB\t%s\n\033[0m",size,$2);}else{size=size/1024;printf("\033[31m%10.3f TB\t%s\n\033[0m",size,$2);}}}}'
第二种方式
cat getsize.js
db = connect("mongodb://ip:27017/default")
var collectionNames= db.getCollectionNames();
for (var i = 0; i < collectionNames.length; i++) {
var coll = db.getCollection(collectionNames[i]);
var stats = coll.stats(1024 * 1024);
print(stats.ns, stats.storageSize);
}
mongo getsize.js