MongoDB1.9.1新功能预告:提示符小优化PS1++
在Shell命令行中,我们大概都知道PS1这个变量的意义,通过修改PS1的值,你可以自由定义你的终端提示符。而在MongoDB的mongo客户端命令行中,也将加入这样的功能,而且更强大,让你的运维管理工作更方便。
最初MongoDB的命令行提示符是简单的”>”,如下
>
后来在最近的版本中,我们看到对Replica Sets的连接提示符做了有意义的变更,像这样
myReplSetName:SECONDARY>
但是我们其实还可以做到更定制化一些,就像Shell环境中的PS1变量一样。嗯,这就是在1.9.1中将出现的一个小功能,MongoDB的核心开发工程师kristina刚刚发了一篇博文,描述了其将在提示符上会进行的改进,并将其称为PS1++,也就是说会比PS1更强大,更具定制化。
下面我们就来看一下会有哪些定制化的功能吧
简单的字符串定义
myReplSetName:SECONDARY> prompt = "> " > > // ah, bliss > > // some sysadmins think > is a weird prompt, as it's also > // used for redirections, so they might prefer $ > prompt = "$ " $ $ // there we go
函数式定义
> prompt = function() { return db+"> "; }
test> use foo
foo> use bar
bar>
更复杂的函数
> states = ["STARTUP", "PRIMARY", "SECONDARY", "RECOVERING", "FATAL",
... "STARTUP2", "UNKNOWN", "ARBITER", "DOWN", "ROLLBACK"]
>
> prompt = function() {
... result = db.isMaster();
... if (result.ismaster) {
... return db+"> ";
... }
... else if (result.secondary) {
... return "("+db+")> ";
... }
... result = db.adminCommand({replSetGetStatus : 1})
... return states[result.myState]+":"+db+"> ";
... }
(test)>
告诉你是否连接的是mongos机器
> prompt = function() {
... result = db.adminCommand({isdbgrid : 1});
... if (result.ok == 1) {
... return "mongos> ";
... }
... return "> ";
... }
告诉你当前的时间
> prompt = function() {
... var now = new Date();
... return now.getHours()+":"+now.getMinutes()+":"+now.getSeconds()+"> ";
... }
10:30:45> db.foo.count()
60000
10:30:46>
还可以定义在配置文件里
$ # load from command line arg:
$ mongo shellConfig.js
MongoDB shell version 1.9.1-
connecting to: test
>
> // load from the shell itself
> load("/path/to/my/shellConfig.js")
另一个新功能是自动加载的配置文件,类似于.bashrc,这个文件会叫.mongorc.js
你可以写你想在连接时执行的内容
// my startup file
prompt = /* ... */
// getting "not master and slaveok=false" errors drives me nuts,
// so I'm overriding the getDB() code to ALWAYS set slaveok=true
Mongo.prototype.getDB = function(name) {
this.setSlaveOk();
return new DB(this, name);
}
/* and so on... */
42区 VPS
42qu.com 云主机 , 卖给创业的你 。 点击这里 , 查看详情
| anyShare分享此文章的同学,将有机会送我new iPad! | |
| |

