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…
- First, we create a simple JavaScript file and call it
hello.js
. Type the following body in thehello.js
file:function sayHello(name) { print('Hello ' + name + ', how are you?') }
- Save this file at the location,
/mongo/scripts/hello.js
. (This can be saved at any other location too.) - On the command prompt, execute the following:
> mongo --shell /mongo/scripts/hello.js
- On executing this, we should see the following printed to our console:
MongoDB shell version: 3.0.2 connecting to: test >
- Test the database that the shell is connected to by typing the following command:
> db
This should print out
test
to the console. - Now, type the following command in the shell:
> sayHello('Fred')
- 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 port27017
. The database connected will bemydb
.mongo.server.host/mydb
: This will connect to the server running onmongo.server.host
and the default port27017
. The database connected will bemydb
.mongo.server.host:27000/mydb
: This will connect to the server running onmongo.server.host
and the port27000
. The database connected will bemydb
.mongo.server.host:27000
: This will connect to the server running onmongo.server.host
and the port27000
. 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:
Option |
Description |
---|---|
|
This shows help regarding the usage of various command-line options. |
|
When the |
|
The specifies the port of the mongo server where the client needs to connect. |
|
This specifies the hostname of the mongo server where the client needs to connect. If the |
|
This is relevant when security is enabled for mongo. It is used to provide the username of the user to be logged in. |
|
This option is relevant when security is enabled for mongo. It is used to provide the password of the user to be logged in. |