mongo
Shellmongo
Shell¶On this page本页内容
Note
The following document pertains to the 以下文档涉及MongoDB服务器下载中包含的mongo
shell included in the MongoDB Server Download.mongo
shell。For information on the new MongoDB Shell, 有关新MongoDB Shell mongosh
, refer to the mongosh Documentation.mongosh
的信息,请参阅mongosh文档。
To understand the differences between the two shells, see Comparison of the mongo Shell and mongosh.要了解这两个Shell之间的差异,请参阅mongo Shell和mongosh的比较。
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
文件中添加对提示的逻辑,以设置每次启动mongo
shell时的提示。
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中定义以下变量:
The prompt would then resemble the following:提示将类似于以下内容:
To create a 要以mongo
shell prompt in the form of <database>@<hostname>$
, define the following variables:<database>@<hostname>$
的形式创建mongo
shell提示符,请定义以下变量:
The prompt would then resemble the following:提示将类似于以下内容:
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
变量:
The prompt would then resemble the following:提示将类似于以下内容:
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中使用自己的编辑器。
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>
使用指定的编辑器进行编辑,如下例所示:
myFunction
:myFunction
:
The command should open the 该命令应打开vim
edit session.vim
编辑会话。When finished with the edits, save and exit 完成编辑后,保存并退出vim
edit session.vim
编辑会话。
mongo
shell, type myFunction
to see the function definition:mongo
shell中,键入myFunction
以查看函数定义:
The result should be the changes from your saved edit:结果应该是保存的编辑所做的更改:
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版本而有所不同,但不会影响代码的语义。
mongo
Shell Batch Sizemongo
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
: