Mongodb-the MONGO Shell, Configure the MONGO shell

Source: Internet
Author: User
Tags function definition mongo shell


Customize the Prompt


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, theMONGOcan 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 theMONGOs Hell.


Customize Prompt to Display number of Operations


For example,to Create aMONGOshell prompt with the number of operations issued in the current session, define th E following variables in theMONGOShell:


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


The prompt would then resemble the following:


1> 
2> 
3> 
Customize Prompt to Display Database and Hostname


To create aMONGOshell prompt in the form of<database>@, define the following V Ariables:


3> prompt = "> "
> host = db.serverStatus().host;
huey
> prompt = function() {
... return db+"@"+host+"$ ";
... }
function () {
return db+"@"+host+"$ ";
}


The prompt would then resemble the following:


[email protected]$ 


Customize Prompt to Display up time and Document Count


To create aMONGOshell Prompt This contains the system up time and the number of documents in the Curre NT database, define the followingpromptvariable in theMONGOShell:


[email protected]$ prompt = "> "
> 
> prompt = function() {
... return "Uptime:"+db.serverStatus().uptime+" Documents:"+db.stats().objects+" > ";
... }
function () {
return "Uptime:"+db.serverStatus().uptime+" Documents:"+db.stats().objects+" > ";
}


The prompt would then resemble the following:


uptime:5897 documents:6 >




Use a External Editor in theMONGOShell


You can use your own editor on theMONGOshell by setting the editorenvironment variable before starting theMONGOshell.


Export Editor=vimmongo


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


    1. Define a function myfunction :
       function MyFunction () {} 
    2. edit the function using your editor:
       edit myFunction 

      The command sh Ould Open THE&NBSP;vim  edit session. When finished with the edits, save and exit vim   Edit session.

    3. in the mongo  shell, type myfunction  to See the function definition:
       myFunction 

      The result should is the changes from your saved edit:

      function myFunction() {
          print("This was edited");
      }


Note: AsMONGOShell interprets code edited in a external editor, it may modify code in functions, depending on the Ja Vascript compiler. ForMONGOcould convertto2or remove comments. The actual changes affect only the appearance of the code and would vary based on the version of JavaScript used but would n OT affect the semantics of the code.





Change theMONGOShell Batch Size


The db.collection.find () method is The JavaScript method to retrieve documents from A collection. THE&NBSP;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-to-the-print up-to-the-first documents that match the query. THE&NBSP;mongo shell would prompt



You can set the "dbquery.shellbatchsizeattribute to" the number of documents from the default valueofas in the following example which sets it to:


Dbquery.shellbatchsize = 10;





Mongodb-the MONGO Shell, Configure the MONGO shell


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.