mongodb : basic commands
Note : This article assumes that mongodb has been installed. If it is not, please refer Install Mongodb
To start the mongodb server, execute the command :
This will start the mongodb server on default port 27017 and create data directory on default locations based on OS used. On windows it is C:\data\db.
To specify a different data directory use –dbpath option with the mongod command.
mongod --dbpath "<<path of data directory>>"
The dbpath argument specifies the path where the data fie will be created.
For example , the following command
mongod --dbpath /home/codereq/mongodb/data
will start the mongodb server on default port 27017 and create data directory at location /home/codereq/mongodb/data
Execute the command :
On executing the above command from the client, you will see a log on server telling that a client has connected.
connection accepted from 127.0.0.1:51363 #1 (1 connection now open)
So , with the above 2 commands our basic setup is complete. Now we will see how to create a database.
mongodb comes with a default database called test
To create database , execute command
For example executing command
will create database testdb01
Once the database is created, the current database is change to newly created one.
- To know which is the current database, execute the db command
- Note : Just after creating the database, show databases will not yet show the database. The database is shown only if there is any collection in the database.
- To drop the database use the following command
In the subsequent articles, we will show how to create collection, create documents some query commands.