博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mongo shell启动配置文件.mongorc.js(三)
阅读量:7137 次
发布时间:2019-06-28

本文共 1599 字,大约阅读时间需要 5 分钟。

自定义MongoDB操作函数

可以把自己写的js代码保存在某个地方,让MongoDB加载它,然后就可以在MongoDB的命令行里操作它们。

mongodb shell默认会加载~/.mongorc.js文件

例如以下修改了启动提示文字、左侧提示文字,增加了my_show_shards shell函数用于显示当前sharded collection的chunks在各分片的负载情况:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//~/.mongorc.js  
//show at begin    
var 
compliment = [
"attractive"
"intelligent"
"like batman"
];    
var 
index = Math.floor(Math.random()*3);    
print(
"Hello, you're looking particularly " 
+ compliment[index] + 
" today!"
);
//change the prompt    
prompt = 
function
(){    
    
if 
(
typeof 
db == 
"undefined"
) {    
        
return 
"(nodb)> "
;    
    
}    
    
// Check the last db operation    
    
try 
{    
        
db.runCommand({getLastError: 1});    
    
}    
    
catch 
(e) {    
        
print(e);    
    
}    
    
return 
db + 
"> "
;    
}
//show all shard's chunks    
function 
my_show_shards() {    
    
var 
config_db = db.getSiblingDB(
"config"
);    
    
var 
collections = {};    
    
var 
shards = {};    
    
var 
shard_it = config_db.chunks.find().snapshot();
    
while 
(shard_it.hasNext()) {  
        
next_item = shard_it.next();    
        
collections[JSON.stringify(next_item[
"ns"
]).replace(/\
"/g, "
")] = 1;    
        
shards[JSON.stringify(next_item["
shard
"]).replace(/\"/g, "
")] = 1;    
    
}    
    
var list_collections = [];    
    
var list_shards = [];    
    
for (item in collections) {    
        
list_collections.push(item);    
    
}    
    
for (item in shards) {    
        
list_shards.push(item);    
    
}
    
list_collections.forEach(function(collec) {  
            
list_shards.forEach(function(item) {    
                
obj = {};    
                
obj["
shard
"] = item;    
                
obj["
ns"] = collec;    
                
it = config_db.chunks.find(obj);    
                
print(collec, item, it.count());    
                
})    
            
})    
}
本文转自UltraSQL51CTO博客,原文链接: http://blog.51cto.com/ultrasql/1707354
,如需转载请自行联系原作者
你可能感兴趣的文章
Cortex系列M0-4简单对比
查看>>
相对定位
查看>>
JAVASCRIPT 类型转换
查看>>
MongoDB入门上
查看>>
B进制星球
查看>>
[mysql] 无法通过insert 创建用户ERROR 1364 (HY000): Field 'ssl_cipher' doesn't have a default value...
查看>>
Ruby初探
查看>>
【移动端】单位em相关资料
查看>>
SQL优化-标量子查询(数据仓库设计的隐患-标量子查询)
查看>>
java 拷贝功能
查看>>
ZOJ2432 Greatest Common Increasing Subsequence 题解报告
查看>>
Cannot find module `express`
查看>>
20051206: 早退
查看>>
Tycho build 1: 构建插件
查看>>
ORACLE HANDBOOK系列之十三:计划任务(Scheduler)
查看>>
凸包问题的描述(Graham法)
查看>>
失败的沟通与成功的沟通
查看>>
学习技术,从何谈起
查看>>
android中按back键返回上一个activity,如何重新调用上一个activity的oncreate方法?...
查看>>
Ubantu 安装boost环境
查看>>