MongoDB服务端JavaScript脚本使用方法

发布时间:2022-04-21 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了MongoDB服务端JavaScript脚本使用方法脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

常用JavaScript语句

复制代码 代码如下:

db.getSiblinGDB(<dbname>)&nbsp; 
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” 博客

脚本宝典总结

以上是脚本宝典为你收集整理的MongoDB服务端JavaScript脚本使用方法全部内容,希望文章能够帮你解决MongoDB服务端JavaScript脚本使用方法所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。