溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MongoDB服務端JavaScript腳本

發(fā)布時間:2020-06-23 06:39:39 來源:網(wǎng)絡(luò) 閱讀:4177 作者:UltraSQL 欄目:MongoDB數(shù)據(jù)庫

MongoDB服務端JavaScript腳本

 

常用JavaScript語句

 

db.getSiblingDB(<dbname>)   
db.getCollectionNames()    
db.getCollection(<collname>)    
db.printCollectionStats()

 

在mongo shell運行JavaScript腳本

 

切換數(shù)據(jù)庫:  

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函數(shù)

 

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í)行。


備注:以上輸出結(jié)果保存為CSV文件打開。


在mongo shell啟動配置文件.mongorc.js中存儲JavaScript函數(shù)


mongodb shell默認會加載~/.mongorc.js文件,可以自定義prompt變量來修改shell提示信息。也可以將上述常用的DBA管理函數(shù)放到這里。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI