EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB Create Database

MongoDB Create Database

By Swati TawdeSwati Tawde

MongoDB Create Database

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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 –

  1. Replication in MongoDB
  2. MongoDB Tools
  3. MongoDB Alternatives
  4. How To Install MongoDB
C++ PROGRAMMING Certification Course
38+ Hours of HD Videos
9 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ASP.NET Certification Course
149+ Hours of HD Videos
28 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SQL - Everything in SQL
253+ Hours of HD Videos
51 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SOFTWARE TESTING Certification Course
74+ Hour of HD Videos
13 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
MONGODB Certification Course
 29+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more