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文件打开。
在mongo shell启动配置文件.mongorc.js中存储JavaScript函数
mongodb shell默认会加载~/.mongorc.js文件,可以自定义prompt变量来修改shell提示信息。也可以将上述常用的DBA管理函数放到这里。
分享标题:MongoDB服务端JavaScript脚本
分享网址:https://www.cdcxhl.com/article10/ghijdo.html
成都网站建设公司_创新互联,为您提供建站公司、、搜索引擎优化、软件开发、App开发、网站导航
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联