Customize the Prompt MongoDB
Use an External Editor in the
Change the
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 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:
The prompt would then resemble the following:
Customize Prompt to Display Database and Hostname
To create a
mongo
shell prompt in the form of @$
, define the following variables:
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:
The prompt would then resemble the following:
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 themongo
shell.
Once in the
mongo
shell, you can edit with the specified editor by typing edit
or edit
, as in the following example:- Define a function
myFunction
: - Edit the function using your editor: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: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. For example, 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. 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 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
: