MongoDB Cookbook(Second Edition)
上QQ阅读APP看书,第一时间看更新

Connecting to a single node in the Mongo shell with JavaScript

This recipe is about starting the mongo shell and connecting to a MongoDB server. Here we also demonstrate how to load JavaScript code in the shell. Though this is not always required, it is handy when we have a large block of JavaScript code with variables and functions with some business logic in them that is required to be executed from the shell frequently and we want these functions to be available in the shell always.

Getting ready

Although it is possible to run the mongo shell without connecting to the MongoDB server using mongo --nodb, we would rarely need to do so. To start a server on the localhost without much of a hassle, take a look at the first recipe, Installing single node MongoDB, and start the server.

How to do it…

  1. First, we create a simple JavaScript file and call it hello.js. Type the following body in the hello.js file:
    function sayHello(name) {
      print('Hello ' + name + ', how are you?')
    }
  2. Save this file at the location, /mongo/scripts/hello.js. (This can be saved at any other location too.)
  3. On the command prompt, execute the following:
    > mongo --shell /mongo/scripts/hello.js
    
  4. On executing this, we should see the following printed to our console:
    MongoDB shell version: 3.0.2
    connecting to: test
    >
    
  5. Test the database that the shell is connected to by typing the following command:
    > db
    

    This should print out test to the console.

  6. Now, type the following command in the shell:
    > sayHello('Fred')
    
  7. You should get the following response:
    Hello Fred, how are you?
    

Note

Note: This book was written with MongoDB version 3.0.2. There is a good chance that you may be using a later version and hence see a different version number in the mongo shell.

How it works…

The JavaScript function that we executed here is of no practical use and is just used to demonstrate how a function can be preloaded on the startup of the shell. There could be multiple functions in the .js file containing valid JavaScript code—possibly some complex business logic.

On executing the mongo command without any arguments, we connect to the MongoDB server running on localhost and listen for new connections on the default port 27017. Generally speaking, the format of the command is as follows:

mongo <options> <db address> <.js files>

In cases where there are no arguments passed to the mongo executable, it is equivalent to the passing of the db address as localhost:27017/test.

Let's look at some example values of the db address command-line option and its interpretation:

  • mydb: This will connect to the server running on localhost and listen for a connection on port 27017. The database connected will be mydb.
  • mongo.server.host/mydb: This will connect to the server running on mongo.server.host and the default port 27017. The database connected will be mydb.
  • mongo.server.host:27000/mydb: This will connect to the server running on mongo.server.host and the port 27000. The database connected will be mydb.
  • mongo.server.host:27000: This will connect to the server running on mongo.server.host and the port 27000. The database connected will be the default database test.

Now, there are quite a few options available on the mongo client too. We will see a few of them in the following table: