常用JavaScript語句
db.getSiblingDB(<dbname>)
db.getCollectionNames()
db.getCollection(<collname>)
db.printCollectionStats()
在mongo shell運行JavaScript腳本
切換數(shù)據(jù)庫:
use <dbname>
運行如下腳本:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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函數(shù)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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上下文中,可以使用該函數(shù)。退出該會話后,該函數(shù)不會被保存。只可在Primary執(zhí)行。
備注:以上輸出結果保存為CSV文件打開。
本文出自 “SQL Server Deep Dives” 博客