在Shell命令行中,我们大概都知道PS1这个变量的意义,通过修改PS1的值,你可以自由定义你的终端提示符。而在MongoDB的mongo客户端命令行中,也将加入这样的功能,而且更强大,让你的运维管理工作更方便。下面学步园小编来讲解下MongoDB1.9.1有哪些定制化的功能?
MongoDB1.9.1有哪些定制化的功能
简单的字符串定义
myReplSetName:SECONDARY>prompt=">"
>
>//ah,bliss
>
>//somesysadminsthink>isaweirdprompt,asit'salso
>//usedforredirections,sotheymightprefer$
>prompt="$"
$
$//therewego
函数式定义
>prompt=function(){returndb+">";}
test>usefoo
foo>usebar
bar>
更复杂的函数
>states=["STARTUP","PRIMARY","SECONDARY","RECOVERING","FATAL",
..."STARTUP2","UNKNOWN","ARBITER","DOWN","ROLLBACK"]
>
>prompt=function(){
...result=db.isMaster();
...if(result.ismaster){
...returndb+">";
...}
...elseif(result.secondary){
...return"("+db+")>";
...}
...result=db.adminCommand({replSetGetStatus:1})
...returnstates[result.myState]+":"+db+">";
...}
(test)>
MongoDB1.9.1有哪些定制化的功能
告诉你是否连接的是mongos机器
>prompt=function(){
...result=db.adminCommand({isdbgrid:1});
...if(result.ok==1){
...return"mongos>";
...}
...return">";
...}
告诉你当前的时间
>prompt=function(){
...varnow=newDate();
...returnnow.getHours()+":"+now.getMinutes()+":"+now.getSeconds()+">";
...}
10:30:45>db.foo.count()
60000
10:30:46>
还可以定义在配置文件里
$#loadfromcommandlinearg:
$mongoshellConfig.js
MongoDBshellversion1.9.1-
connectingto:test
>
>//loadfromtheshellitself
>load("/path/to/my/shellConfig.js")
另一个新功能是自动加载的配置文件,类似于.bashrc,这个文件会叫.mongorc.js
你可以写你想在连接时执行的内容
//mystartupfile
prompt=/*...*/
//getting"notmasterandslaveok=false"errorsdrivesmenuts,
//soI'moverridingthegetDB()codetoALWAYSsetslaveok=true
Mongo.prototype.getDB=function(name){
this.setSlaveOk();
returnnewDB(this,name);
}
/*andsoon...*/
以上就是关于“MongoDB1.9.1有哪些定制化的功能”的内容,希望对大家有用。更多资讯请关注学步园。学步园,您学习IT技术的优质平台!