C:\MongoDB\Bin> mongo MongoDB shell version: 3.2.9 connecting to: test >
You will also see a welcome message and help and support options the first time you invoke mongo shell.
By default, the mongo shell tries to connect to a database server on localhost listening on port 27017 which is the default port. To connect to a different host or port, use the --host and --port options.
To connect to a MongoDB instance running on host 10.121.65.17 and port 27020 :
C:\MongoDB\Bin> mongo --host 10.121.65.17 --port 27020 MongoDB shell version: 3.2.9 connecting to: 10.121.65.17:27020/test >
mongo shell establishes connection to a MongoBD instance and sets the current database to test by default. However, when you start mongo shell, you could also specify database address to connect to. The database address contains the hostname or IP address followed by the / character and the database name. If hostname or IP address is omitted, mongo shell assumes the host is localhost.
To connect to a database named student on the local host, run
To connect to the student database on a remote machine, run
You can switch the database at anytime from a mongo shell session with the command
use dbname. For example to change the current database to student
> use student switched to db student
To view the current database run the
> db student
In MongoDB, the databases are created on the fly. There are no commands available that lets you create a database manually. You could select a new database that does not exist and MongoDB creates this database when a collection or document is inserted into it.
To list all databases on the server, run the command
show databases or
> show dbs local 0.078125GB mydb 0.203125GB test 0.203125GB >
To list all collections in the current database, run the command
> show collections myCollection system.indexes >
Collections are created implicitly when a document is inserted. You can also create a collection manually with the db.createCollection() method.
In mongo shell the current database is referenced using the variable db and a collection is referenced in the form db.CollectionName. If the collection that is referenced does not exist, it will be created implicitly when a document is inserted.