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文件内容如下:

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
49
50
51
52
53
;( 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























本文转自UltraSQL51CTO博客,原文链接: http://blog.51cto.com/ultrasql/1707359,如需转载请自行联系原作者



相关文章
|
1月前
|
搜索推荐 Shell Linux
【Shell 命令集合 系统管理 】Linux 管理用户配置文件 userconf命令 使用指南
【Shell 命令集合 系统管理 】Linux 管理用户配置文件 userconf命令 使用指南
37 2
|
8月前
PM2 配置文件(ecosystem.config.js 字段详细介绍)
PM2 配置文件(ecosystem.config.js 字段详细介绍)
329 0
|
3月前
|
Web App开发 缓存 前端开发
VUE-CLI可选的配置文件vue.config.js
VUE-CLI可选的配置文件vue.config.js
29 0
|
1月前
|
Shell Linux C语言
【Shell 命令集合 网络通讯 】Linux 验证Samba配置文件 testparm命令 使用教程
【Shell 命令集合 网络通讯 】Linux 验证Samba配置文件 testparm命令 使用教程
35 0
|
1月前
|
缓存 Shell Linux
【Shell 命令集合 磁盘管理 】Linux 配置文件系统的相关设置 fsconf命令使用指南
【Shell 命令集合 磁盘管理 】Linux 配置文件系统的相关设置 fsconf命令使用指南
23 0
|
6月前
|
存储 Shell Linux
Shell命令切换root用户、管理配置文件、检查硬件
  与其他基于UNIX的系统一样,Linux也可以被多个人同时使用。多用户功能能够让多人在单个Linux系统上拥有账户,并且保护自己的数据不被他人破坏。
154 0
|
8月前
|
运维 Shell Linux
【运维知识高级篇】超详细的Shell编程讲解1(Shell作用+脚本书写方式+脚本执行方式+变量分类+变量配置文件+变量定义+Shell重要的位置变量+三种传参方式)
【运维知识高级篇】超详细的Shell编程讲解1(Shell作用+脚本书写方式+脚本执行方式+变量分类+变量配置文件+变量定义+Shell重要的位置变量+三种传参方式)
261 1
|
Java Shell Linux
解决shell脚本中"source /etc/profile"重载配置文件不生效的问题
1、shell脚本中“source /etc/profile”无法生效的原因及解决办法;2、shell中"."、"source"、"sh"、"./"的区别;
解决shell脚本中"source /etc/profile"重载配置文件不生效的问题
|
监控 Shell
Shell脚本监控mongo并自动重启
Shell脚本监控mongo并自动重启
144 0
Mongo Shell 常用查询命令
一、find 命令进行简查询 find( 查询条件 ,返回的字段), 1. 查询时返回所有字段 db.user.find() --> 查询user集合中所有的数据