我是靠谱客的博主 拉长皮带,这篇文章主要介绍MongoDB服务端JavaScript脚本使用方法,现在分享给大家,希望可以做个参考。

常用JavaScript语句

复制代码 代码如下:
复制代码

db.getSiblingDB(<dbname>)  
db.getCollectionNames()   
db.getCollection(<collname>)   
db.printCollectionStats()

在mongo shell运行JavaScript脚本
 
切换数据库:  

复制代码 代码如下:
复制代码

use <dbname>

运行如下脚本:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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函数

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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脚本使用方法内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(119)

评论列表共有 0 条评论

立即
投稿
返回
顶部