• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

MongoDB服务端JavaScript脚本使用方法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

常用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” 博客


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
如何在Oracle中导入dmp文件发布时间:2022-02-08
下一篇:
教你如何6秒钟往MySQL插入100万条数据的实现发布时间:2022-02-08
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap