MongoDB服务端中怎么使用JavaScript脚本-成都快上网建站

MongoDB服务端中怎么使用JavaScript脚本

本篇文章给大家分享的是有关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执行。

以上就是MongoDB服务端中怎么使用JavaScript脚本,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


名称栏目:MongoDB服务端中怎么使用JavaScript脚本
链接分享:http://kswjz.com/article/gegpcd.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流