首页 > 数据库技术 > 详细

MongoDB服务端JavaScript脚本

时间:2015-10-22 14:23:14      阅读:249      评论:0      收藏:0      [点我收藏+]

MongoDB服务端JavaScript脚本

 

常用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” 博客,请务必保留此出处http://ultrasql.blog.51cto.com/9591438/1705153

MongoDB服务端JavaScript脚本

原文:http://ultrasql.blog.51cto.com/9591438/1705153

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!