
MongoDB Cookbook - Second Edition
By :

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.
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.
hello.js
. Type the following body in the hello.js
file:function sayHello(name) { print('Hello ' + name + ', how are you?') }
/mongo/scripts/hello.js
. (This can be saved at any other location too.)> mongo --shell /mongo/scripts/hello.js
MongoDB shell version: 3.0.2 connecting to: test >
> db
This should print out test
to the console.
> sayHello('Fred')
Hello Fred, how are you?
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.
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:
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. |
Change the font size
Change margin width
Change background colour