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 a MongoDB 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”.
Here, test is a system database.
Today, we will learn the following database commands:
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
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.
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
ExampleIn 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:
Now we are executing the “show dbs” command and the output will be as in the following:
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.
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.
Db Command
The db command shows the current selected database name.
Syntax: db
Let's see an example.
If a database is not selected, then the “db” command will show the “test” database name.
test is the 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.
If we want to delete the Demo_Test database, then first select the Demo_Test database and then run the dropDatabase() command.
Now check the database list again.
We can see that the Demo_Test database does not exist in the preceding list. In other words, this database has been deleted.
Note:
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!