---
# System prepended metadata

title: countDB

---

# countDB
```
#!/bin/bash

new_ip="127.0.0.1"
new_port="27017"
new_database=""
new_root=""
new_root_password=""
new_authdb="admin"

RESULT=`/var/vcap/packages/mongodb/bin/mongo --quiet --host $new_ip --port $new_port $new_database --username $new_root --password $new_root_password --authenticationDatabase $new_authdb --eval "printjson(db.getCollectionNames())"`

#echo $RESULT
RESULT=${RESULT//\[/}
RESULT=${RESULT//\]/}
RESULT=${RESULT//\"/}

IFS=', ' read -a collectlist <<< $RESULT

len=$((${#collectlist[@]}-1))
echo $len
for var in $(seq 0 $len)
do
  count=`/var/vcap/packages/mongodb/bin/mongo --quiet --host $new_ip --port $new_port $new_database --username $new_root --password $new_root_password --authenticationDatabase $new_authdb --eval "printjson(db['${collectlist["$var"]}'].count())"`
  printf "Collection名稱: %-45s 資料數量 %10s 筆\n" ${collectlist["$var"]} $count
done
```

```
#!/bin/bash

new_ip="127.0.0.1"
new_port="27017"
new_database=""
new_root=""
new_root_password=""
new_authdb="admin"

RESULT=`/var/vcap/packages/mongodb/bin/mongo --quiet --host $new_ip --port $new_port $new_database --username $new_root --password $new_root_password --authenticationDatabase $new_authdb --eval "printjson(db.getCollectionNames())"`

#echo $RESULT
RESULT=${RESULT//\[/}
RESULT=${RESULT//\]/}
RESULT=${RESULT//\"/}

IFS=', ' read -a collectlist <<< $RESULT

len=$((${#collectlist[@]}-1))
echo $len
for var in $(seq 0 $len)
do
  #count=`/var/vcap/packages/mongodb/bin/mongo --quiet --host $new_ip --port $new_port $new_database --username $new_root --password $new_root_password --authenticationDatabase $new_authdb --eval "printjson(db['${collectlist["$var"]}'].count())"`
  #printf "Collection名稱: %-45s 資料數量 %10s 筆\n" ${collectlist["$var"]} $count
  size=`/var/vcap/packages/mongodb/bin/mongo --quiet --host $new_ip --port $new_port $new_database --username $new_root --password $new_root_password --authenticationDatabase $new_authdb --eval "printjson(db['${collectlist["$var"]}'].stats().wiredTiger[\"block-manager\"][\"file bytes available for reuse\"])"`
  printf "Collection名稱: %-50s 可回收資料量 %20s \n" ${collectlist["$var"]} $(($size/1024/1024))
done
```

```
new_ip=""
new_port=""
new_database=""
new_root=""
new_root_password=""

export PGPASSWORD=$new_root_password
for sch in `psql -h $new_ip -p $new_port -U $new_root -qAt -c "SELECT n.nspname AS "Name" FROM pg_catalog.pg_namespace n WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema';" $new_database` ;
do
    for tbl in `psql -h $new_ip -p $new_port -U $new_root -qAt -c "select tablename from pg_tables where schemaname='$sch';" $new_database` ;
    do
    count=`psql -h $new_ip -p $new_port -U $new_root -qAt -c "select count(*) from \"$sch\".\"$tbl\"" $new_database` ;
    printf "Table名稱: %-45s 資料數量 %10s 筆\n" $sch.$tbl $count
    #echo $sch.$tbl的資料數量 $count 筆
    done
done
```
mongodb 一般方式
```
db[collectionName].count();
```
mongodb 資料大小排序
```
var collectionNames = db.getCollectionNames(), stats = [];
collectionNames.forEach(function (n) { stats.push(db[n].stats()); });
stats = stats.sort(function(a, b) { return b['size'] - a['size']; });
for (var c in stats) { print(stats[c]['ns'] + ": " + stats[c]['size'] + " (" + stats[c]['storageSize'] + ")"); }
```