Updated April 20, 2023
Introduction to MongoDB Create Database
MongoDB Create Database is the process and syntax of creating MongoDB database and collections. MongoDB is an Open Source and document-based NoSQL database. MongoDB depends upon the structure of the collection, and it manages automatically. There are specific commands associated with MongoDb to create and access the database, such as the ‘use’ command, which helps switch to a particular MongoDB database. There is a named ‘db’ used to verify the present database name in MongoDB. Similarly to create a collection ‘db.createCollection(<CollectionName>,<Options>)’ function syntax is being used in MongoDB. Additionally, there are commands available to show and delete a particular database or a collection in MongoDB.
Creating a MongoDB Database
If you are from the SQL background, you might think as in SQL, create database command is used in MongoDB to create a database. MongoDB does not support any command to create a database. In MongoDB, there is no need 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?
To create a database in MongoDB, we should first place the database and collection correctly. The database is for storing all the collection. And those collections are used for storing all the docents. Documents are nothing but a collection of field names and values.
Let us see some example to understand the structure of collections.
{
"StudentID": 1,
"StudentName": "John"
}
In the above example, there are two fields, i.e. StudentID and StudentName. Therefore, document contains these two fields as a field name and 1 and John as field values.
Create a Database
To create a database in MongoDB, use command is used
Syntax:
use DatabaseName
For Example:
Use Student
In the above example, the use is a command to create a database, and the Student is the name of the database.
If the above command executed successfully, then it gives the following output.
Output: Switched to Student
MongoDB automatically switched to the database once the user creates it.
Note: If the name you give to the database already exists, then MongoDB will redirect you, i.e. connect you directly to the database. If the name you give to the database does not exist, it creates the database of the given name and then switched to the created database.
Show the Name of the Database
If you want to know the name of the database you are connecting to currently, you can use db command to know the name of the database.
For Example:
db
Output: Student
This command helps users when they are working with multiple databases and want to know the right database to insert the values in the database.
List of all the Databases
If you want to know all the databases present in MongoDB, you can use the show DBS command to list all the databases.
For Example:
show dbs
Output:
Teacher
Employees
In the above-mentioned example, you can see that the database Student is not shown in the databases list. This is because MongoDB does not create the database until the documents are saved.
How to Create a Collection?
To create collections in the database i.e document which contains field name and field values we use MongoDB db.createCollection(name, options) command.
Syntax of createCollection() command is given as follows:
db.createCollection(name, options)
- In this command, the name parameter is the name of the collection which we want to create.
- An option is an optional parameter. An option is a document that is used to specify the configuration of the collection.
Steps to create Collections
Below are some steps for creating a collection which is as follows:
Step 1:
Use db.createCollection command.
db.createCollection("CreateCollection")
The output of the above-mentioned command can be fetched by using the show collections command.
show collections
Output:
CreateCollection
Also, MongoDB creates collections automatically when you insert some documents. So the above-mentioned steps can be eliminated.
The following steps can be followed.
Step 1: First, write the insert statement to add documents into the collection
Syntax:
db.DatabaseName.insert
(
);
Step 2: After that, write the field names and field values under the insert statement
Syntax:
db.DatabaseName.insert
(
{
field name1,
field Value1
}
{
field name2
field value2
}
...
....
);
For Example:
db.Student.insert
(
{
"StudentID": 1,
"StudentName": "smith"
}
);
MongoDB provides insert() command to add documents into the collections of the database.
If the above command is successfully executed, it will give the following output.
Output:
WriteResult({“nInserted”: 1})
The above output shows that the insert command is successfully executed and adds 1 record to the database’s collection.
Now, if u run the show dbs command, it will give the following output.
For Example:
show dbs
Output:
Student
Teacher
Employees
Delete the Created Database
If you want to delete the created database, you can make use of the drop command. MongoDB provides a drip command to delete the database.
Syntax
db.DropDatabase()
This will delete the database which you have selected. If a specific database is not selected, then it will delete the default database.
For Example:
Step 1: First, check the available databases by using the show DBS command
show dbs
student
Teacher
Employees
Test
Step 2: If you want to delete the Student database, then commands will be as follows:
db.dropDatabase()
{ "dropped" : "student", "ok" : 1 }
Step 3: The result can be checked using the show DBS command.
show dbs
Teacher
Employees
Test
Drop a Collection from the Database
In MongoDB, db.collection.drop() command is used to drop a collection from the database.
Syntax of drop() command:
db.collection_name.drop()
For Example:
Step 1: First, check the available collections by using the show collections command
show collections
CreateCollection
TeacherCollection
EmployeesCollection
Step 2: If you want to delete CreateCollection, then commands will be as follows:
db.CreateCollection.drop()
true
Step 3: The result can be checked using the show collections command.
show collections
TeacherCollection
EmployeesCollection
Conclusion – MongoDB Create Database
In this article, we have seen how to create an ad drop database as well as create and delete collections in MongoDB by using MongoDB commands.
Recommended Articles
This has been a guide to MongoDB Create Database. Here we have covered basic concepts, lists of all databases, and how to create, delete, drop, and delete collections in MongoDB using MongoDB commands. You can also go through our other suggested articles to learn more –