Running MongoDB and Some Basic Commands

1. Running MongoDB

You need to remember two commands from the console
mongod: This command is use to start mongo dbservice
mongo: This command is used as CLI tool for MongoDB

1.1 Run MongoDB

The following shows how to run MongoDB:

 In Windows:
 D:\>mongod.exe
 Or on UNIX/ Linux:
 home@maverick$ mongod
 You can also mention the DIR for DB Storage
 D:\>mongod.exe --dbpathd:\test\mongodb\data

Mongo Service Starts

1.2 Start writing commands with MongoDB CLI

The following shows how to start writing commands with MongoDB:

C:\Users\Parth Joshi>d:

D:\>mongo
MongoDB shell version: 3.0.4
connecting to: test
>

2. Some basic commands in MongoDB

2.1 Show Databases

The following code snipet displays the command in MongoDB on how to show database:

> show dbs
Displays the databases for current instance of MongoDB database
D:\>mongo
MongoDB shell version: 3.0.4
connecting to: test
> show dbs
local 0.078GB
testdb1 0.078GB
>

2.2 Create or use databases

The following code snipet displays the command in MongoDB on how to create or use database:
> use [dbname]
Once you use a database, if it is not created it will be created.
But it will be displayed only after first collections is created
Example:

> show dbs
local 0.078GB
testdb1 0.078GB
> use firstdb
switched to dbfirstdb
> db.testcoll.insert({"name":"ParthJoshi"});
WriteResult({ "nInserted" : 1 })
> show dbs
firstdb0.078GB
local 0.078GB
testdb1 0.078GB
>

2.3 Drop Database

You have to use the db first to try this command in MongoDB:

db.dropdatabase()

This command removes the Database. You can see the effect by typing show dbs to see the list of Databases.