在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技術的優質平台!