MongoDB

Starting MongoShell / MongoClient

The mongo shell is an interactive JavaScript interface to MongoDB Server. We can use the mongo shell to query and update data as well as perform administrative operations.

The mongo shell is a component of the MongoDB distributions. Once we have installed and have started MongoDB server (mongod), we can connect the mongo shell to our running MongoDB instance.
Starting The Mongo Shell:-
Basic Syntax of mongo command is as follows
mongo <options>

Options are optional but options are useful in some cases, let’s view some key options.

--portSpecifies the port where the mongod or mongos instance is listening. If --port is not specified, mongo attempts to connect to port 27017.
-- hostSpecifies the name of the host machine where the mongod or mongos is running. If this is not specified, mongo attempts to connect to a MongoDB process running on the localhost.
<dbname>Specifies the “database name” of the database to connect.
ex:- mongo workdb
<host:port>Specifies the connection with host and port details, direct usage of –host, --port
ex:- mongo 192.168.0.7:27017

Some Example ways to Start the mongo shell Open the terminal and go to MongoDB installation directory.

  1. starting mongo shell without any options
    • mongo
  2. starting mongo shell to connect to host ‘mongo.tutorialtous.com’ with port 23396
    • mongo –host mongo.tutorialtous.com –port 23396
    • or
    • Mongo mongo.tutorialtous.com:23396
  3. Starting mongo shell to connect to ‘test’ database
    • mongo test