mongodb java 执行js脚本_MongoDB服务端JavaScript脚本使用方法

常用JavaScript语句

db.getSiblingDB()

db.getCollectionNames()

db.getCollection()

db.printCollectionStats()

在mongo shell运行JavaScript脚本

切换数据库:

use

运行如下脚本:

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/ 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” 博客


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部