Configure the mongo Shell

Customize the Prompt

You may modify the content of the prompt by setting the variableprompt in the mongo shell. The prompt variable canhold strings as well as JavaScript code. If prompt holds a functionthat returns a string, mongo can display dynamic informationin 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.

Customize Prompt to Display Number of Operations

For example,to create a mongo shell prompt with the numberof operations issued in the current session, define the followingvariables in the mongo shell:

  1. cmdCount = 1;
  2. prompt = function() {
  3. return (cmdCount++) + "> ";
  4. }

The prompt would then resemble the following:

  1. 1>
  2. 2>
  3. 3>

Customize Prompt to Display Database and Hostname

To create a mongo shell prompt in the form of<database>@<hostname>$, define the following variables:

  1. host = db.serverStatus().host;
  2.  
  3. prompt = function() {
  4. return db+"@"+host+"$ ";
  5. }

The prompt would then resemble the following:

  1. test@myHost1$

Customize Prompt to Display Up Time and Document Count

To create a mongo shell prompt that contains the systemup time and the number of documents in the current database,define the following prompt variable in the mongoshell:

  1. prompt = function() {
  2. return "Uptime:"+db.serverStatus().uptime+" Documents:"+db.stats().objects+" > ";
  3. }

The prompt would then resemble the following:

  1. Uptime:5897 Documents:6 >

Use an External Editor in the mongo Shell

You can use your own editor in the mongo shell by settingthe EDITOR environment variable before starting themongo shell.

  1. export EDITOR=vim
  2. mongo

Once in the mongo shell, you can edit with the specifiededitor by typing edit <variable> or edit <function>, as in thefollowing example:

  • Define a function myFunction:
  1. function myFunction () { }
  • Edit the function using your editor:
  1. edit myFunction

The command should open the vim edit session. When finishedwith the edits, save and exit vim edit session.

  • In the mongo shell, type myFunction to see thefunction definition:
  1. myFunction

The result should be the changes from your saved edit:

  1. function myFunction() {
  2. print("This was edited");
  3. }

Note

As mongo shell interprets code edited in an externaleditor, it may modify code in functions, depending on theJavaScript compiler. For example, mongo may convert 1+1 to2 or remove comments. The actual changes affect only theappearance of the code and will vary based on the version ofJavaScript 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 toretrieve documents from a collection. Thedb.collection.find() method returns a cursor to theresults; however, in the mongo shell, if the returned cursoris not assigned to a variable using the var keyword, then thecursor is automatically iterated up to 20 times to print up to thefirst 20 documents that match the query. The mongo shellwill prompt Type it to iterate another 20 times.

You can set the DBQuery.shellBatchSize attribute to change thenumber of documents from the default value of 20, as in thefollowing example which sets it to 10:

  1. DBQuery.shellBatchSize = 10;