Mongo Shell

Mongo shell is an interactive JavaScript interface to MongoDB used to perform administrative operations and also query and update data.

Prior to connecting to MongoDB, ensure that MongoDB is running. If it is not, start MongoDB.

To start MongoDB, run the following command in a terminal.

Windows

C:\> "C:\Program Files\MongoDB\Server\4.0\bin\mongod.exe"

Ubuntu

sudo service mongod start

Start Mongo Shell

Once you are sure that MongoDB is running,

Windows

Open another Command Window and run the following command.

C:\> "C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe"

Ubuntu

Open another Terminal and run the following command to start mongo shell.

mongo
arjun@arjun-VPCEH26EN:~$ mongo
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.9
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	http://docs.mongodb.org/
Questions? Try the support group
	http://groups.google.com/group/mongodb-user
Server has startup warnings: 
2017-10-15T12:35:20.897+0530 I STORAGE  [initandlisten] 
2017-10-15T12:35:20.897+0530 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2017-10-15T12:35:20.897+0530 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2017-10-15T12:35:22.336+0530 I CONTROL  [initandlisten] 
2017-10-15T12:35:22.336+0530 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-10-15T12:35:22.337+0530 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-10-15T12:35:22.337+0530 I CONTROL  [initandlisten] 
2017-10-15T12:35:22.337+0530 I CONTROL  [initandlisten] 
2017-10-15T12:35:22.337+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-10-15T12:35:22.337+0530 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-10-15T12:35:22.337+0530 I CONTROL  [initandlisten] 
2017-10-15T12:35:22.337+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-10-15T12:35:22.337+0530 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-10-15T12:35:22.337+0530 I CONTROL  [initandlisten] 
>

If the server is not running, you may get connect failed message as below :

arjun@arjun-VPCEH26EN:~$ mongo
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017
2017-10-15T12:25:19.606+0530 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2017-10-15T12:25:19.667+0530 E QUERY    [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:237:13
@(connect):1:6
exception: connect failed

When no parameters are provided with mongo command, the default functionality is that, the mongo shell tries to make a connection to the MongoDB server running at localhost on port 27017.

But if you like to connect to MongoDB server that is running on a different machine connected to your network, you may make use of the options of mongo shell as shown below

mongo --host <host> --port <port_number>

An example is provided below

mongo --host 192.168.0.104 --port 28019

Now we shall run a simple query db to know the database the shell is pointing to.

> db
test

test is a default database.

ADVERTISEMENT

Start Mongo Shell for a specified MongoDB instance among multiple instances

If you come across the scenario that there are multiple MongoDB instances running in a same machine, but on the different ports of-course, then to connect to a particular MongoDB instance differentiated by the port it is running on, run the following command :

mongo --port <port_number>

Following is an example demonstrating to open a Mongo Shell connected to a MongoDB instance running at 27018.

mongo --port 27018

Mongo Shell Commands

Following are a useful list of Mongo Shell Commands :

  • help – show help
  • help admin – administrative help
  • help connect – connecting to a db help
  • help keys – key shortcuts
  • help misc – misc things to know
  • help mr – mapreduce
  • show dbs – show database names
  • show collections – show collections in current database
  • show users – show users in current database
  • show profile – show most recent system.profile entries with time >= 1ms
  • show logs – show the accessible logger names
  • show log [name] – prints out the last segment of log in memory, ‘global’ is default
  • use <db_name> – set current database
  • it – result of the last line evaluated; use to further iterate
  • exit – quit the mongo shell

Conclusion

In this MongoDB Tutorial, we have learnt about Mongo Shell, its usage and its connectivity to MongoDB Server when the server is running in another machine in network.