Using the Mongo Shell: Getting Started

In the following subsections we shall set up the environment including installing the required software.

We shall start Mongo shell and connect with MongoDB server. And we will also discuss running a command in Mongo shell.

1. Setting Up the Environment

Download and install the following software if not already installed from Chapter 1:

  • MongoDB (3.0.5) for Windows 64-bit is used in this chapter. Download binary distribution from

Double-click on the MongoDB binary distribution to install MongoDB. Add the bin directory of the MongoDB installation to the PATH environment. Create a directory C:\data\db for the MongoDB data. Start the MongoDB server with the following command in a Command window.


The MongoDB server gets started as shown in Figure 2-1.

2. Starting the Mongo Shell

Open a new Command window. Start the Mongo shell with the following command.


The Mongo shell gets started and gets connected to the test database by default as shown in Figure 2-2.
By default the Mongo shell connects to MongoDB on
localhost on port 27017.

A message similar to “Hotifx KB2731284 or later update is not installed, will zero-out data files” might get generated. One of the reasons for the message is that MongoDB is installed in a directory such that the directory path has spaces, for example, directory path C:\Program Files\MongoDB\Server\3.0. The message may be ignored if directory path with spaces is the cause of the message. If the reason for the message is that the C:\data\db directory has not been created, create the directory C:\data\db.

To start the Mongo shell without connecting to any database, run the following command. Before running the following command, open a new command window as we already connected once using the mongo command.

>mongo -nodb

Subsequently a connection may be opened to MongoDB server using the JavaScript Mongo() constructor, which by default connects to localhost at port 27017.

>connection=new Mongo()

The getDB() method in Mongo shell may be used to set the database as shown in Figure 2-3. db=connection.getDB(“test”)

As a result of the preceding commands the Mongo shell is connected to MongoDB server on the default host localhost and on the default port 27017 with the test database in use, which is the same if just the mongo command is run. Connecting using the Mongo() constructor is useful if connecting to a non-default host and port.

Alternatively, the connect method may be used to connect to MongoDB in Mongo shell. After starting Mongo shell with -nodb option, connect to MongoDB on localhost and port 27017 and set database as test with the following command.

db = connect(“localhost:27017/test”);

The connect() method gets connected to MongoDB test database as shown in Figure 2-4.

3. Running a Command or Method in Mongo Shell

The following kinds of commands and methods may be run in Mongo shell.

  • The database commands.
  • Mongo shell JavaScript helper methods
  • Mongo shell help methods

The difference between the JavaScript helper methods and the help methods is that the JavaScript helper methods make use of JavaScript. The database commands have the BSON document format consisting of key/value pairs with the first key being the name of the command and subsequent key/value pairs being the command options. For example, the create command, which is used to create a collection, has the following syntax.

{ create: <collection_name>, option1:value, option2:value, option3:value,..optionN:value }

The create command provides the following options (only the main options are discussed).

  •  capped. Set to boolean value (true or false) to specify if the collection is a capped collection. Default value is false. If true is set, the size option is also required. A capped collection is a fixed size collection in which the earlier documents are overwritten when the maximum size is reached.
  •  autoIndexId. Set to boolean value (true or false) to specify if an automatic index is to be created on the _id field. The default value is true.
  • size. The maximum size in bytes of a capped collection. Required for a capped collection.
  • max. Maximum number of documents in a capped collection. The size setting takes precedence over the max setting. For example, if size is 3 and max is 4, the maximum number of documents is 3.

The database commands may be run using the Mongo shell helper method db.runCommand(). For example the create command may be run to create a collection “mongo” using the following helper/ wrapper JavaScript method in Mongo shell.

db.runCommand({ create: “mongo” })

A command response is a JSON document with at least the ok field, which indicates if the command succeeded as shown in Figure 2-5. A value of 1 indicates the command succeeded and a value of 0 indicates the command failed.

The create command is one of those commands that have an equivalent JavaScript shell helper method, the db.createCollection() method. The mongo collection could have been created as follows.


The preceding command returns a JSON document with ok field set to 1, which indicates the command succeeded as shown in Figure 2-6.

If the preceding command is to be run subsequent to the db.runCommand to create the mongo collection, the collection must be deleted using the db.mongo.drop() command to avoid the “collection already exists” error message, which is shown in Figure 2-7. (See “Dropping a Collection” later in this chapter.)

A complete reference of the database commands is available at, and a complete reference of Mongo shell helper methods is available at

JavaScript methods may also be run using a JavaScript file. For example, copy the following JavaScript to a file connection.js in the directory C:\MongoDB from which the command is to be run.

connection = new Mongo();

db = connection.getDB(“test”);


From the command line run the JavaScript file with the following command.

>mongo connection.js

The JavaScript gets evaluated and the output gets generated, which is a listing of collection names for the example JavaScript as shown in Figure 2-8.

The Mongo shell also provides some help methods to get information about the databases, collections, and documents. Some of the help methods are listed in following table, Table 2-1.

Source: Vohra Deepak (2015), Pro MongoDB™ Development, Apress; 1st ed. edition.

Leave a Reply

Your email address will not be published. Required fields are marked *