MongoDB Create Database is that the process and syntax of making MongoDB database and collections. It is an Open Source and document-based NoSQL database. MongoDB depends upon the structure of the gathering , and it manages automatically. There are specific commands related to MongoDb to make and access the database, like the ‘use’ command, which helps switch to a specific MongoDB database. So, lets see how to create a database in MongoDB.

There’s a named ‘db’ wont to verify this database name in MongoDB. Similarly to make a set ‘db.createCollection(,)’ function syntax is getting used in MongoDB. Additionally, there are commands available to point out and delete a specific database or a set in MongoDB.

Creating a MongoDB Database

If you’re from the SQL background, you would possibly think as in SQL, create database command is employed in MongoDB to make a database. MongoDB doesn’t support any command to make a database. In MongoDB, there’s no got to create a database manually as in SQL; MongoDB creates the database automatically by retrieving values from the defined collection when the user stores that value in collections.

How to create a Database in MongoDB?

Create a Database in MongoDB

To create a database in MongoDB, we should always first place the database and collection correctly. The database is for storing all the gathering . and people collections are used for storing all the docents. Documents are nothing but a set of field names and values.

Let us see some example to know the structure of collections.

{
"ID": 7,
"Name" : " Shubham"
}

You can see that there are two fields namely ID and Name containing the values 7 and Shubham respectively.

Create a Database

To create a database in MongoDB, the “use” command is employed

Syntax :

use DataBase_Name

# To take an example
use Employee

In the above sample, we have used the use command to utilize the Employee database.

If the above command executed successfully, then it gives the subsequent output.

Output: Switched to Employee

Thus, MongoDB by itself, switches to the desired database once you create one.

How to Show the Name of the Database

If you would like to understand the name of the database you’re connecting currently, you’ll use db command to understand the name of the database.

# Command used
db

#Output received
Output : Employee

This command helps users once they are working with multiple databases and need to understand the proper database to insert the values within the database.

List of all the Databases

If you would like to understand all the databases present in MongoDB, you’ll use the “show dbs” command to list all the databases.

#Command used
show dbs

#Output
Student
Staff
Teacher

In the above-mentioned example, you’ll see that the database Employee isn’t shown within the databases list. This is often because MongoDB doesn’t create the database until the documents are saved.

Here’s an article on creating Web Server using Python

How to Create a Collection?

To create collections within the database i.e document which contains field name and field values we use MongoDB db.createCollection(name, options) command.

Syntax :

db.createCollection(name, options

In this command, the name parameter is that the name of the gathering which we would like to make . An option is an optional parameter. An option may be a document that’s wont to specify the configuration of the gathering .

Steps to Create Collections

  • Use the db.createCollection command.
db.createCollection("CreateCollection")

#For output .... use the command
show collections

#Output received
CreateCollection

Also, MongoDB creates collections automatically once you insert some documents. Therefore, the above-mention steps are undergo elimination.

The following steps need to be follow.

  • Write the insert statement to feature documents into the gathering

Syntax :

db.DatabaseName.insert
(
);
  • Thereafter, write the sector names and field values under the insert statement

Syntax :

db.DatabaseName.insert
(
{
field name1,
field Value1
}
{
field name2
field value2
}
{
field name3
field value3
}
…
….
);

Now, lets take an example :

db.Employee.insert
(
{
"ID": 5
"Name": "Ayush"
}
);

MongoDB provides insert() command to feature documents into the collections of the database. If the above command successfully executes, it’ll thus, give the subsequent output.

Output:

WriteResult({“nInserted”: 1})

The above output shows that the insert command successfully executes and adds 1 record to the database’s collection.

Now, if u run the show dbs command, it’ll give the subsequent output.

show dbs

# Output :
Student
Staff
Teacher
Employee

Now, how to Delete a Database you created ?

If you would like to delete a database you created, then you’ll need to use the drop command. MongoDB provides a drip command to delete the database.

Syntax

db.DropDatabase()

This will also delete the database which you’ve select. If a select database isn’t select, then it’ll delete the default database.

For Example:

  • Let’s check the available databases by using the “show dbs”command
show dbs

#Output :
Student
Staff 
Teacher
Employee
  • If you would like to delete the Student database, thus, the command’s going to be as follows:
db.dropDatabase()
{ "dropped" : "student", "ok" : 1 }
  • To check the result, we may again use the “show dbs” command :
show dbs

#Output :
Staff
Teacher
Employee

SUMMING UP

In this guide, we have got ourselves an upstart on MongoDB. We have seen how to create a database in MongoDB, we have also seen the use of various other commands like the “show dbs” command, the “use” command and the “drop” command to name a few. We also dealt with collections. Rest assured, next-up we are going to discuss various other features and commands of MongoDB in greater depth. Till then go carefully, through this article. You can also munch on other articles on our website.

Categorized in: