源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

MongoDB服务端JavaScript脚本使用方法

  • 时间:2022-10-29 00:30 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:MongoDB服务端JavaScript脚本使用方法
常用JavaScript语句
[u]复制代码[/u] 代码如下:
db.getSiblingDB(<dbname>)   db.getCollectionNames()    db.getCollection(<collname>)    db.printCollectionStats()
在mongo shell运行JavaScript脚本   切换数据库:  
[u]复制代码[/u] 代码如下:
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下运行  
[u]复制代码[/u] 代码如下:
mongo localhost:27017/<dbname> dbaStatCollections.js
或在mongo shell下运行   
[u]复制代码[/u] 代码如下:
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)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部