In this part of the Learn Mongo Series, we will learn how to create a database in mongodb. We will be creating a
database with the name demo_db
.
Well , you will find it strange but there is no command to create a database in mongodb however we can start using a database in mongodb. And when we start using a database , it will be automatically created. Let's go through the whole process step by step.
show dbs
command as shown below :
> show dbs
> show dbs
activityoverload 0.203125GB
animals 0.203125GB
clonecs 0.203125GB
local 0.078125GB
test 0.203125GB
>
use <database_name>
command as shown below :
> use demo_db
> use demo_db
switched to db demo_db
>
show dbs
commands list of databases displayed below :
> show dbs
> show dbs
activityoverload 0.203125GB
animals 0.203125GB
clonecs 0.203125GB
local 0.078125GB
test 0.203125GB
>
show dbs
command does not display empty databases. SO we need to add some content in our database
using the following command db.<collection_name>.insert({"key":"value"})
as shown below :
> use demo_db
switched to db demo_db
> db.details.insert(
...{
... "Website":"nodejsera"
...})
>show dbs
show dbs
which will produce the following output :
> show dbs
activityoverload 0.203125GB
animals 0.203125GB
clonecs 0.203125GB
demo_db 0.203125GB
local 0.078125GB
test 0.203125GB
>
In this part of learn mongo series , we learned about how we can create a database in mongodb. We learned the following commands of mongodb :
use :
This command is use to either create a new database or switch to an already existing database. show dbs :
This command is used to list all the non-empty mongodb databases on the console