常用JavaScript語句
復制代碼 代碼如下:
db.getSiblingDB(<dbname>)
db.getCollectionNames()
db.getCollection(<collname>)
db.printCollectionStats()
在mongo shell運行JavaScript腳本
切換數據庫:
復制代碼 代碼如下:
use <dbname>
運行如下腳本:
var total = 0;
var dbaStatCollections = function(){};
dbaStatCollections = function(){
collNames = db.getCollectionNames();
for (var index = 0; index < collNames.length; index++) {
var coll = db.getCollection(collNames[index]);
var stats = coll.stats();
print('ns,count,size,totalIndexSize');
print(stats.ns + ',' + stats.count + ',' + stats.size + ',' + stats.totalIndexSize);
}
}
dbaStatCollections();
可將上述腳本保存為dbaStatCollections.js,
在linux shell下運行
復制代碼 代碼如下:
mongo localhost:27017/<dbname> dbaStatCollections.js
或在mongo shell下運行
復制代碼 代碼如下:
load("dbaStatCollections.js")
在服務端存儲JavaScript函數
db.system.js.remove({"_id":"dbaStatCollections"});
db.system.js.save(
{
_id : "dbaStatCollections" ,
value : function () {
collNames = db.getCollectionNames();
for (var index = 0; index < collNames.length; index++) {
var coll = db.getCollection(collNames[index]);
var stats = coll.stats();
print('ns,count,size,totalIndexSize');
print(stats.ns + ',' + stats.count + ',' + stats.size + ',' + stats.totalIndexSize);
}
}
}
);
db.loadServerScripts();
dbaStatCollections();
在當前JavaScript上下文中,可以使用該函數。退出該會話後,該函數不會被保存。只可在Primary執行。
備注:以上輸出結果保存為CSV文件打開。
本文出自 “SQL Server Deep Dives” 博客