Configure the mongo Shell-官方文档摘录
Posted 熊出陌
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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, 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 themongo
shell.
大概的意思:你可以通过修改mongorc.js这个文件来让你的终端提示符变得不太一样
注意:
# vim /root/.mongorc.js
如果使用yum 装的,这个文件在/root/目录下
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;
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:
host = db.serverStatus().host;
prompt = function() {
return db+"@"+host+"$ ";
}
The prompt would then resemble the following:
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:
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
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:
-
Define a function
myFunction
:function myFunction () { }
-
Edit the function using your editor:
edit myFunction
The command should open the
vim
edit session. When finished with the edits, save and exitvim
edit session. -
In the
mongo
shell, typemyFunction
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.
以上大概的意思是:通过集中方式来设置显示的方式,需要注意的是mong shell的解释器会依赖于js的版本,即使你使用了编辑器,还是依靠于js的解释器。它只会影响代码的展现而不会影响代码本身的意思
Change the mongo
Shell Batch Size
The db.collection.find()
method is the JavaScript method to retrieve documents from a collection. Thedb.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 Typeit
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;
大概的意思是:因为在mongo shell中,默认只会将前面20条的结果显示出来,修改默认的显示迭代次数,可以使用
DBQuery.shellBatchSize来设置
以上是关于Configure the mongo Shell-官方文档摘录的主要内容,如果未能解决你的问题,请参考以下文章
MongoDB - The mongo Shell, Access the mongo Shell Help
MongoDB - The mongo Shell, Write Scripts for the mongo Shell
MongoDB - The mongo Shell, Data Types in the mongo Shell
MongoDB - The mongo Shell, mongo Shell Quick Reference