Customize the Prompt

You may modify the content of the prompt by setting the variable prompt in the mongo shell. The promptvariable can hold strings as well as JavaScript code. If prompt holds a function that returns a string, mongo can display dynamic information in each prompt.

You can add the logic for the prompt in the .mongorc.js file to set the prompt each time you start up the mongo shell.

Customize Prompt to Display Number of Operations

For example,to create a mongo shell prompt with the number of operations issued in the current session, define the following variables in the mongo shell:

> cmdCount = 1;
1
> prompt = function() {
... return (cmdCount++) + "> ";
... }
function () {
return (cmdCount++) + "> ";
}

The prompt would then resemble the following:

1>
2>
3>

Customize Prompt to Display Database and Hostname

To create a mongo shell prompt in the form of <database>@<hostname>$, define the following variables:

3> prompt = "> "
> host = db.serverStatus().host;
huey
> prompt = function() {
... return db+"@"+host+"$ ";
... }
function () {
return db+"@"+host+"$ ";
}

The prompt would then resemble the following:

test@huey$ 

Customize Prompt to Display Up Time and Document Count

To create a mongo shell prompt that contains the system up time and the number of documents in the current database, define the following prompt variable in the mongo shell:

test@huey$ prompt = "> "
>
> prompt = function() {
... return "Uptime:"+db.serverStatus().uptime+" Documents:"+db.stats().objects+" > ";
... }
function () {
return "Uptime:"+db.serverStatus().uptime+" Documents:"+db.stats().objects+" > ";
}

The prompt would then resemble the following:

Uptime:5897 Documents:6 >

Use an External Editor in the mongo Shell

You can use your own editor in the mongo shell by setting the EDITOR environment variable before starting the mongo shell.

export EDITOR=vim
mongo

Once in the mongo shell, you can edit with the specified editor by typing edit <variable> or edit <function>, as in the following example:

  1. Define a function myFunction:

    function myFunction () { }
  2. Edit the function using your editor:
    edit myFunction

    The command should open the vim edit session. When finished with the edits, save and exit vim edit session.

  3. In the mongo shell, type myFunction to see the function definition:
    myFunction

    The result should be the changes from your saved edit:

    function myFunction() {
    print("This was edited");
    }

NOTE: As mongo shell interprets code edited in an external editor, it may modify code in functions, depending on the JavaScript compiler. For mongo may convert 1+1 to 2 or remove comments. The actual changes affect only the appearance of the code and will vary based on the version of JavaScript used but will not affect the semantics of the code.

Change the mongo Shell Batch Size

The db.collection.find() method is the JavaScript method to retrieve documents from a collection. The db.collection.find() method returns a cursor to the results; however, in the mongo shell, if the returned cursor is not assigned to a variable using the var keyword, then the cursor is automatically iterated up to 20 times to print up to the first 20 documents that match the query. The mongo shell will prompt Type it to iterate another 20 times.

You can set the DBQuery.shellBatchSize attribute to change the number of documents from the default value of 20, as in the following example which sets it to 10:

DBQuery.shellBatchSize = 10;

MongoDB - The mongo Shell, Configure the mongo Shell的更多相关文章

  1. MongoDB的安装,mongod和mongo的区别

    一. mongoDB安装路径 安装路径(最新4.0.11):https://www.mongodb.com/download-center/community?jmp=nav 建议另外找路径下载,外网 ...

  2. MongoDB学习笔记:文档Crud Shell

    MongoDB学习笔记:文档Crud Shell   文档插入 一.插入语法 db.collection.insertOne() 将单个文档插入到集合中.db.collection.insertMan ...

  3. 1.Configure the mongo Shell-官方文档摘录

    Customize the Prompt 自定义提示 You may modify the content of the prompt by setting the variable prompt i ...

  4. MONGODB(三)——Java操作Mongo

    相比于java调用MySqlApI来操作数据库,调用Mongo要简洁容易的多.通过一个简单的样例,很容易地就可以上手 一.导入Jar包 添加Monog支持Java的jar包,这里使用的是2.9.3 & ...

  5. 交互式shell和非交互式shell、登录shell和非登录shell的区别

    交互式shell和非交互式shell.登录shell和非登录shell的区别.首先,这是两个不同的维度来划分的,一个是是否交互式,另一个是是否登录. 交互式shell和非交互式shell(intera ...

  6. linux中的shell脚本编程---初识shell

    Shell是用户与Linux或Unix内核通信的工具,shell编程指的并不是编写这个工具,而是指利用现有的shell工具进行编程,写出来的程序是轻量级的脚本,我们叫做shell脚本. Shell的语 ...

  7. Ubuntu下把缺省的dash shell修改为bash shell

    Ubuntu下缺省使用的是shell是dash,而不是bash.从/bin/sh软连接的指向可以看出这点. dash shell 虽然比bash shell更轻便,但是它并不支持所有的语法,运行she ...

  8. Shell编程-11-子Shell和Shell嵌套

    目录 什么是子Shell 子Shell产生的途径 Shell脚本调用模式 什么是子Shell     子Shell的概念其实是贯穿整个Shell的,如果想要更好的理解和写Shell脚本则必须要了解子S ...

  9. [shell]shell 中| && || () {} 用法以及shell的逻辑与或非

    转自:https://www.jianshu.com/p/617c1ee1e46e | 运算符 管道符号,是unix一个很强大的功能,符号为一条竖线:"|".用法: command ...

随机推荐

  1. 收集计算机分区信息,去除列中的重复值(Excel)(空行)

    收集计算机分区信息 $servers = gc D:\serverlist.txt $result = $results = $null $results = @() foreach ($server ...

  2. Python 条件语句

    Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. Python程序语言指定任何非0和非空(null)值为true,0 或者 null为false. Py ...

  3. UVA 573 (13.08.06)

     The Snail  A snail is at the bottom of a 6-foot well and wants to climb to the top.The snail can cl ...

  4. CSS3 Animation 基于 less 构建的 css3 动画库

    LESS动画优点 · 快速开发css3动画 · 采用less mixins写法,不会生成冗余css · 已加入主流浏览器前缀,保证最大兼容性 · LESS-Animation 部分mixins支持传参 ...

  5. 【JavaScript】常用的JS

    JQuery.UnderScoreJS.Zepto 原生JS等

  6. Citrix 服务器虚拟化之二十八 XenApp6.5发布文档内容

    Citrix 服务器虚拟化之二十八  XenApp 6.5发布文档内容 XenApp可发布以下类型的资源向用户提供信息访问,这些资源可在服务器或桌面上虚拟化: 1)  服务器桌面:发布场中服务器的整个 ...

  7. android自定义TabWidget样式

    先看看效果图吧,个人觉得图标丑了点,不过还行,自己用PS做的 下面是全部代码和流程,一定要按流程顺序来,不然错误! 1.tabhost.xml <TabHost xmlns:android=&q ...

  8. 表ADT

    表一般不用简单数组来实现,通常将其实现为链表.在链表中要不要使用表头则属于个人兴趣问题.在下面的例程中我们都使用表头. 按照C的约定,作为类型的List(表)和Position(位置)以及函数的原型都 ...

  9. QT 操作数据库

    整理一下 QT 操作数据库的一些要点,以备以后的查询学习(主要是操作 mysql ). 首先,要查询相关的驱动是否已经装好了,可以用以下的程序进行验证: #include <QtCore/QCo ...

  10. 基于linux2.6.38.8内核zImage文件的自解压详解

    转载:http://blog.csdn.net/wavemcu/article/details/7270439 ******************************************** ...