跳到主要内容

MongoDB 常用命令

统计表大小

执行 use 命令,进入到要查询的库中

use 库名
  • 库名请修改为实际要进入的库名称,例如:use mdworkflow
function getReadableFileSizeString(fileSizeInBytes) {
 var i = -1;
 var byteUnits = [' kB', ' MB', ' GB', ' TB', 'PB', 'EB', 'ZB', 'YB'];
 do {
 fileSizeInBytes = fileSizeInBytes / 1024;
 i++;
 } while (fileSizeInBytes > 1024);

 return Math.max(fileSizeInBytes, 0.1).toFixed(1) + byteUnits[i];
};
var collectionNames = db.getCollectionNames(), stats = [];
collectionNames.forEach(function (n) { stats.push(db[n].stats()); });
stats = stats.sort(function(a, b) { return b['count'] - a['count']; });
for (var c in stats) { print(stats[c]['ns'] + " , " + stats[c]['count'] + " ," + getReadableFileSizeString(stats[c]['storageSize']) + ""); }
  • 执行完以上命令,会统计并输出当前库中各表的大小