Configure the mongo Shell配置mongo Shell

On this page本页内容

Note

The following document pertains to the mongo shell included in the MongoDB Server Download.以下文档涉及MongoDB服务器下载中包含的mongo shellFor information on the new MongoDB Shell, mongosh, refer to the mongosh Documentation.有关新MongoDB Shell mongosh的信息,请参阅mongosh文档。

To understand the differences between the two shells, see Comparison of the mongo Shell and mongosh.要了解这两个Shell之间的差异,请参阅mongo Shell和mongosh的比较

Customize the Prompt自定义提示

You may modify the content of the prompt by setting the variable prompt in the mongo shell.您可以通过在mongo shell中设置变量prompt来修改提示的内容。The prompt variable can hold strings as well as JavaScript code.prompt变量可以保存字符串和JavaScript代码。If prompt holds a function that returns a string, mongo can display dynamic information in each prompt.如果prompt包含一个返回字符串的函数,mongo可以在每个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.你可以在.mongorc.js文件中添加对提示的逻辑,以设置每次启动mongoshell时的提示。

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:例如,要使用当前会话中发出的操作数创建mongo shell提示符,请在mongo shell中定义以下变量:

cmdCount = 1;
prompt = 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:要以<database>@<hostname>$的形式创建mongo shell提示符,请定义以下变量:

host = db.serverStatus().host;

prompt = function() {
             return db+"@"+host+"$ ";
         }

The prompt would then resemble the following:提示将类似于以下内容:

test@myHost1$

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:要创建包含系统运行时间和当前数据库中文档数的mongo shell提示,请在mongo shell中定义以下prompt变量:

prompt = 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 Shellmongo Shell中使用外部编辑器

You can use your own editor in the mongo shell by setting the EDITOR environment variable before starting the mongo shell.通过在启动mongo shell之前设置editor环境变量,可以在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:进入mongo shell后,可以通过键入edit <variable>edit <function>使用指定的编辑器进行编辑,如下例所示:

  1. Define a function myFunction:定义函数myFunction

    function myFunction () { }
  2. Edit the function using your editor:使用编辑器编辑函数:

    edit myFunction

    The command should open the vim edit session.该命令应打开vim编辑会话。When finished with the edits, save and exit vim edit session.完成编辑后,保存并退出vim编辑会话。

  3. In the mongo shell, type myFunction to see the function definition:mongo shell中,键入myFunction以查看函数定义:

    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.mongo shell解释在外部编辑器中编辑的代码时,它可能会修改函数中的代码,具体取决于JavaScript编译器。For example, mongo may convert 1+1 to 2 or remove comments.例如,mongo可以将1+1转换为2或删除注释。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.实际更改只影响代码的外观,并且会根据所使用的JavaScript版本而有所不同,但不会影响代码的语义。

Change the mongo Shell Batch Size更改mongo Shell批大小

The db.collection.find() method is the JavaScript method to retrieve documents from a collection.db.collection.find()方法是从集合中检索文档的JavaScript方法。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;