MongoDB - Day 3 (Database Basics)

This article shows how to create a database in MongoDB and use some other basic commands. A MongoDB deployment holds a number of databases.  A database holds a set of collections that function as the equivalent of relational database tables. Each collection holds a set of documents. Documents are a set of key-value pairs. This is the basic intro to MomgoDB database.

Now we start work on databases. First open a command prompt and run the “mongod” command. Now open another command prompt and run the “mongo” command.

When you run the mongo command you will see the message “connecting to:test”.

mongo command

Here test is a system database.

Today we will learn the following database commands:
  • Use
  • db
  • show dbs
  • db.dropDatabase()

Now we will see each command individually.

use

This command creates a database or selects a database. If the database name already exists then it will be switched to an existing database otherwise it will  create a new database.

Syntax: use Database_Name

Example

create a new database

In this example we can see that the Demo database already exists in the list.  So we switched to the Demo database that already exists.

Let us see another example.

another example

We can see that the Demo2 database does not exist in the preceding list. So use the command to create a new database.

The “switched to db Demo2” message shows that we have been switched to the Demo2 database.

show dbs

This command is used to show the database name.

Syntax: show dbs

Example

see example

In the preceding example we run the “show dbs” command and retrieve all the database names that exist. Now we see another example.

First of all create a new database <Demo_Test> as in the following:

new database

Now we are executing the “show dbs” command and the output will be as in the following:

see database

We can see that the Demo_Test database is not present in the preceding list. Now the question is, why is our database not visible? The reason is that we created an empty database. So the size of the database is zero.

Now we create a collection in the Demo_Test database and then again execute the “show dbs” command.

show dbs command

Now we can see that the Demo_Test database is present in the preceding list.

Note: We can see that the size of our database is 0.078 GB. This is the minimum (default) size of any database. the size of the database will increase when the size of the existing data exceeds 0.078 GB.

data exceed

Db Command

The db command shows the current selected database name.

Syntax: db

Let us see an example.

example

If a database is not selected then the “db” command will show the “test” database name. test is the system database.

test is system database

dropDatabase Command

The dropDatabase() command deletes the selected database. First select a database and then use the dropDatabase() command.

Syntax: db.dropDatabase()

Example

First we check the list of available databases using the command show dbs command.

show dbs

If we want to delete the Demo_Test database then first select the Demo_Test database and then run the dropDatabase() command.

dropDatabase

Now check the database list again.

database list

We can see that the Demo_Test database does not exist in the preceding list. In other words, this database has been deleted.

Note:
  • MongoDB is case sensitive, if we write “Use” instead of “use” then MongoDB will throw an error.

    commands

  • All commands return, at a minimum, a document with an OK field indicating whether the command has succeeded. Failed commands return the OK field with a value of 0.

    cmd

I think this is enough for today. In the next article I will explain another topic. Please provide your valuable suggestions and ask questions if you have any concerns.

Thanks!