mongo shell启动配置文件.mongorc.js(四)
admin
2023-04-11 17:43:29
0

mongo shell启动配置文件.mongorc.js(四)


~/.mongorc.js文件描述如下:


#mongorc.js  
===============

我的 mongorc.js 文件.

提供:

  - `pretty()` 默认使用pretty()帮助方法查询  
  - `ugly()` 帮助方法    
  - prompt 显示与服务端类型相关的信息


## Pretty

mongo shell 有一个帮助函数叫做 `pretty()` 用于美化结果集。 使用这个 mongorc.js 文件默认启用pretty行为。

    > db.marioGames.find()  
    {    
      "_id" : ObjectId("507333d49c25fa3b6e62174d"),    
      "name" : "Super Mario Bros",    
      "super" : true,    
      "release" : ISODate("1985-09-13T07:00:00Z")    
    }    
    {    
      "_id" : ObjectId("5073347b9c25fa3b6e62174e"),    
      "name" : "Super Mario Bros 2",    
      "super" : true,    
      "release" : ISODate("1988-10-09T07:00:00Z")    
    }    
    {    
      "_id" : ObjectId("5073348f9c25fa3b6e62174f"),    
      "name" : "Super Mario Bros 3",    
      "super" : true,    
      "release" : ISODate("1990-02-09T08:00:00Z")    
    }


## Ugly

现在我们默认得到pretty的结果集合,我们偶尔会需要之前的行为(打印文档到单行)。  
通过使用 `ugly()` 帮助方法。方法:

    > db.marioGames({ super: true }).ugly();  
    { "_id" : ObjectId("507333d49c25fa3b6e62174d"), "name" : "Super Mario Bros", "super" : true, "release" : ISODate("1985-09-13T07:00:00Z") }    
    { "_id" : ObjectId("5073347b9c25fa3b6e62174e"), "name" : "Super Mario Bros 2", "super" : true, "release" : ISODate("1988-10-09T07:00:00Z") }    
    { "_id" : ObjectId("5073348f9c25fa3b6e62174f"), "name" : "Super Mario Bros 3", "super" : true, "release" : ISODate("1990-02-09T08:00:00Z") }


## Prompt

默认的prompt现在显示与连接的服务端相关的信息。

####replSet

```  
replSetName:ServerState|database>    
```

####mongos

```  
mongos|host:port|database>    
```

####mongod

```  
mongod|host:port|database>    
```


## Installation

    git clone git@github.com:aheckmann/mongorc.js.git  
    cd mongorc.js    
    make install

它拷贝 .mongorc.js 文件到你的HOME路径主目录。  
如果另一个 .mongorc.js 文件已经存在,它会被重命名为 .mongorc.js.old


## Uninstall

    cd mongorc.js  
    make uninstall

如果 ~/.mongorc.js.old 存在,它将重命名为 ~/.mongorc.js


## Licence

MIT


~/.mongorc.js文件内容如下:

;(function () {
/**  
* Make all queries pretty print by default.    
*/
DBQuery.prototype._prettyShell = true
/**  
* Allow opting into the default ugly print mode.    
*/
DBQuery.prototype.ugly = function () {  
  this._prettyShell = false;    
  return this    
}
/**  
* Override the default prompt to display info related    
* to type of server we connected to.    
*    
* @return {String}    
*/
prompt = function () {  
  var res = rs.status();    
  if (!res || res.errmsg) {    
    // not in a replica set    
    var status = db.serverStatus();    
    return status.process + "|" + status.host + "|" + db + "> ";    
  }
  return replsetPrompt();  
}
/**  
* Creates a prompt string for replSets    
*    
* @return {String}    
*/
function replsetPrompt () {  
  var status;    
  var admin = db.getSiblingDB("admin");    
  var info = admin.runCommand({ replSetGetStatus: 1, forShell: 1});
  if (info.ok) {  
    var state = "";    
    // do we need this?    
    info.members.some(function (member) {    
      if (member.self) {    
        state = member.stateStr;    
        return true;    
      }    
    });    
    status = info.set + ":" + (state || info.myState);    
  } else if (info.info && info.info.length < 20) {    
    // < 20 seems like a hack ??    
    status = info.info;    
  }
  return status + "|" + db + "> "  
}
})();

 

参见:https://github.com/aheckmann/mongorc.js

相关内容

热门资讯

魏平政出线几率高!国民党整合拼... 海峡导报综合报道 国民党迟未公布彰化县长人选,地方盛传,周三国民党中常可能会有答案,引发关注。对此,...
人均奖金600万,知道内存为什... 唯物的中国芯片产业深度观察AI带动存储狂飙,韩国存储大厂SK海力士也跟着起飞。近日,有投行预测其年终...
“洪迪厄斯”号邮轮最后一批转运... △当地时间5月11日,荷兰埃因霍温,身着防护服的人员在机场载有“洪迪厄斯”号乘客和船员的飞机旁工作当...
特朗普当场问:谁喜欢万斯?谁喜... 美国副总统万斯和国务卿鲁比奥谁会成为2028年共和党总统候选人备受关注,总统特朗普5月11日在白宫的...
实探杭州“被电商拉黑”街道:随... 据扬子晚报报道,5月6日,一名网友在社媒发帖称,自己在某电商平台看中了一款商品,商品界面却显示其地址...
大连女子烧烤店内用铁签喂狗,十... 5月11日,一段网传视频显示,女子让宠物狗坐在辽宁大连一家烧烤店的卡座上,并用铁签将肉串喂给宠物狗,...
沃尔核材获得实用新型专利授权:... 证券之星消息,根据天眼查APP数据显示沃尔核材(002130)新获得一项实用新型专利授权,专利名为“...
科技伦理审查助力人工智能产业高... 转自:证券日报     本报记者 郭冀川     随着AI(人工智能)技术迭代加速,相关伦理审查的重...
捷昌驱动获得实用新型专利授权:... 证券之星消息,根据天眼查APP数据显示捷昌驱动(603583)新获得一项实用新型专利授权,专利名为“...
2026云手机为什么这么火?这... 你是否也有过这样的念头:想试试云手机,但一看价格,月付几十甚至上百元,立马打了退堂鼓?又或者,你曾贪...