MongoDB - The mongo Shell, Configure the mongo Shell

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:

[email protected]$ 

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:

[email protected]$ 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;
时间: 2024-08-06 11:56:30

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

简单shell脚本(mongo)

# #监控mongodb进程 # #author:fanyin # # COMMAND=$(ps -ef|grep mongod |awk '{print $8}'|awk '/mongod/{print $1}') #echo $COMMAND #echo ${#COMMAND} function check(){ if [ '/data/mongodb/bin/mongod' = ${COMMAND} ] then echo "mongo is dead " |mail -s  $

MongoDB的安装,mongod和mongo的区别

一. mongoDB安装路径 安装路径(最新4.0.11):https://www.mongodb.com/download-center/community?jmp=nav 建议另外找路径下载,外网太慢,等不住.. 这是一位博主提供的下载路径(4.0.10): 百度链接:https://pan.baidu.com/s/1xhFsENTVvU-tnjK9ODJ7Ag 密码:ctyy 二. 安装步骤 正常的安装步骤 1. 勾选协议,然后Next 2. 选择"Custom"自定义安装,不要

Configure the mongo Shell-官方文档摘录

Customize the Prompt 自定义提示 You may modify the content of the prompt by setting the variable prompt in the mongo shell. The prompt variable can hold strings as well as JavaScript code. If prompt holds a function that returns a string, mongocan display

shell脚本:通过shell实现linux用户管理和监控

学习shell做的第一个脚本,感谢云知梦李强强老师的shell编程教程 创建shell脚本文件: touch menu.sh touch index.sh touch welcome.sh 赋予脚本文件可执行权限: chmod a+x menu.sh index.sh welcome.sh menu.sh #!/bin/bash #menu.sh function menu(){ title="My Home" name="Randy" time=`date +%Y

第三部分shell编程3(shell脚本编写1)

做监控和备份最多 1. shell脚本是什么它是一种脚本语言,并非编程语言可以使用一些逻辑判断.循环等语法可以自定义子函数是系统命令的集合shell脚本可以实现自动化运维,大大增加我们的工作效率 第一个shell脚本:mkdir shellcd shell/vim 1.sh``#! /bin/bash`#This is a test shell script``echo "123456."`touch aming.111`date 2. shell脚本结构以及执行方法开头行指定bash

几个shell程序设计小知识(shell常识部分)

[转自]http://blog.chinaunix.net/uid-168249-id-2860686.html 引用:一.用户登陆进入系统后的系统环境变量:  $HOME 使用者自己的目录  $PATH 执行命令时所搜寻的目录  $TZ 时区  $MAILCHECK 每隔多少秒检查是否有新的信件  $PS1 在命令列时的提示号  $PS2 当命令尚未打完时,Shell 要求再输入时的提示号  $MANPATH man 指令的搜寻路径 二.特殊变量: $0 这个程序的执行名字  $n 这个程序的

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

交互式shell和非交互式shell.登录shell和非登录shell的区别.首先,这是两个不同的维度来划分的,一个是是否交互式,另一个是是否登录. 交互式shell和非交互式shell(interactive shell and non-interactive shell)交互式模式就是在终端上执行,shell等待你的输入,并且立即执行你提交的命令.这种模式被称作交互式是因为shell与用户进行交互.这种模式也是大多数用户非常熟悉的:登录.执行一些命令.退出.当你退出后,shell也终止了.s

交互式与非交互式,登录shell与非登录shell

交互式shell和非交互式shell.登录shell和非登录shell的区别.首先,这是两个不同的维度来划分的,一个是是否交互式,另一个是是否登录. 交互式shell和非交互式shell(interactive shell and non-interactive shell)交互式模式就是在终端上执行,shell等待你的输入,并且立即执行你提交的命令.这种模式被称作交互式是因为shell与用户进行交互.这种模式也是大多数用户非常熟悉的:登录.执行一些命令.退出.当你退出后,shell也终止了.s

shell编程培训之shell的工作原理

Shell是用户和Linux操作系统之间的接口.Linux中有多种shell,其间缺省运用的是Bash.本章叙述了shell的作业原理,shell的品种,shell的一般操作及Bash的特性. 什么是shell Linux系统的shell作为操作系统的外壳,为用户提供使用操作系统的接口.它是命令语言.命令解释程序及程序设计语言的统称. Shell是用户和Linux内核之间的接口程序,如果把Linux内核想象成一个球体的中心,shell就是围绕内核的外层.当从shell或其他程序向Linux传递命