iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >mongo shell启动配置文件.mongorc.js(四)
  • 166
分享到

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

2024-04-02 19:04:59 166人浏览 安东尼
摘要

mongo shell启动配置文件.mongorc.js(四)~/.monGorc.js文件描述如下:#mongorc.js   ===============我的 mongorc.js 文件.提

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 () {

DBQuery.prototype._prettyShell = true

DBQuery.prototype.ugly = function () {  
  this._prettyShell = false;    
  return this    
}

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();  
}

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

您可能感兴趣的文档:

--结束END--

本文标题: mongo shell启动配置文件.mongorc.js(四)

本文链接: https://www.lsjlt.com/news/40094.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作