Configure themongoShell

On this page

Customize the Prompt

You may modify the content of the prompt by setting the variablepromptin themongoshell. Thepromptvariable can hold strings as well as JavaScript code. Ifpromptholds a function that returns a string,mongocan display dynamic information in each prompt.

You can add the logic for the prompt in the.mongorc.jsfile to set the prompt each time you start up themongoshell.

Customize Prompt to Display Number of Operations

For example,to create amongoshell prompt with the number of operations issued in the current session, define the following variables in themongoshell:

  1. cmdCount
  2. =
  3. 1
  4. ;
  5. prompt
  6. =
  7. function
  8. ()
  9. {
  10. return
  11. (
  12. cmdCount
  13. ++
  14. )
  15. +
  16. "
  17. >
  18. "
  19. ;
  20. }

The prompt would then resemble the following:

  1. 1
  2. >
  3. 2
  4. >
  5. 3
  6. >

Customize Prompt to Display Database and Hostname

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

  1. host
  2. =
  3. db
  4. .
  5. serverStatus
  6. ().
  7. host
  8. ;
  9. prompt
  10. =
  11. function
  12. ()
  13. {
  14. return
  15. db
  16. +
  17. "@"
  18. +
  19. host
  20. +
  21. "$ "
  22. ;
  23. }

The prompt would then resemble the following:

  1. test@myHost1$

Customize Prompt to Display Up Time and Document Count

To create amongoshell prompt that contains the system up time_and_the number of documents in the current database, define the followingpromptvariable in themongoshell:

  1. prompt
  2. =
  3. function
  4. ()
  5. {
  6. return
  7. "Uptime:"
  8. +
  9. db
  10. .
  11. serverStatus
  12. ().
  13. uptime
  14. +
  15. " Documents:"
  16. +
  17. db
  18. .
  19. stats
  20. ().
  21. objects
  22. +
  23. "
  24. >
  25. "
  26. ;
  27. }

The prompt would then resemble the following:

  1. Uptime
  2. :
  3. 5897
  4. Documents
  5. :
  6. 6
  7. >

Use an External Editor in themongoShell

You can use your own editor in themongoshell by setting theEDITORenvironment variable_before_starting themongoshell.

  1. export
  2. EDITOR
  3. =
  4. vim
  5. mongo

Once in themongoshell, you can edit with the specified editor by typingedit<variable>oredit<function>, as in the following example:

  1. Define a functionmyFunction:

    1. function
    2. myFunction
    3. ()
    4. {
    5. }
  2. Edit the function using your editor:

    1. edit
    2. myFunction

    The command should open thevimedit session. When finished with the edits, save and exitvimedit session.

  3. In themongoshell, typemyFunctionto see the function definition:

    1. myFunction

    The result should be the changes from your saved edit:

    1. function
    2. myFunction
    3. ()
    4. {
    5. print
    6. (
    7. "This was edited"
    8. );
    9. }

NOTE

Asmongoshell interprets code edited in an external editor, it may modify code in functions, depending on the JavaScript compiler. Formongomay convert1+1to2or 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 themongoShell Batch Size

Thedb.collection.find()method is the JavaScript method to retrieve documents from acollection. Thedb.collection.find()method returns acursorto the results; however, in themongoshell, if the returned cursor is not assigned to a variable using thevarkeyword, then the cursor is automatically iterated up to 20 times to print up to the first 20 documents that match the query. Themongoshell will promptTypeitto iterate another 20 times.

You can set theDBQuery.shellBatchSizeattribute to change the number of documents from the default value of20, as in the following example which sets it to10:

  1. DBQuery
  2. .
  3. shellBatchSize
  4. =
  5. 10
  6. ;