MongoDB服务端JavaScript脚本使用方法
更新时间:2015年10月24日 23:29:53 作者:UltraSQL
这篇文章主要介绍了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” 博客
相关文章
浅谈MySQL和MariaDB区别(mariadb和mysql的性能比较)
MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品2018-02-02
MongoDB快速入门笔记(六)之MongoDB的文档修改操作
这篇文章主要介绍了MongoDB快速入门笔记(六)之MongoDB的文档修改操作的相关资料,需要的朋友可以参考下2016-06-06


最新评论