Updated July 7, 2023
Introduction to MongoDB Database
MongoDB database is defined as a created database for database operations; using MongoDB database, we are creating the collection into it. MongoDB is not a structured database, so we have not used create database statements which we have used in other databases like MySQL and PostgreSQL. We can create the database in MongoDB by using the database and using the keyword, it is used to create a new database in MongoDB; after using the database, we are creating the collection into the same. We can create multiple databases on a single MongoDB server default database that we have used in MongoDB is db.
How to Create Database in MongoDB?
Basically, we have used the use command to create a new database in MongoDB.
Below is the syntax :
Syntax:
Use name_of_database
- In the above syntax, use keyword specifies that create a new database. We can create any new database by using use keywords.
- The name of the database shows the database name that we are creating in MongoDB. We can create a database by specifying the name of the database parameter.
- If using the database is exist on the server it will use the existing database instead of creating the new database.
The below example shows that create a new database:
Code:
use db_test
db
Output:
- In the above example, we have created a database name as db_test after creating, we can see the connected database name by using the db command.
- We can show all the created and system databases by using the show dbs command.
The below example shows that list as follows:
Code:
show dbs
Output:
- After using a database, it will not show in the show dbs command; after creating a collection or object of the database then, it will show using the show dbs command.
The below example shows the same:
Code:
use db_test1
show dbs
db.test.insert({"name":"ABC"})
show dbs
Output:
- In the above first example, we have created a database named db_test1 it will show a connected database by using the db command but not show in the list of databases, because we have not created any collection or object into the db_test1 database.
- In the second example, we have created one collection at the time of data insertion after creating the collection into the db_test1 database; the name of the database is shown using the show dbs command.
- Next time after using the use db_test1 command, we have automatically connected to the db_test1 database.
- Admin and local databases are the system database which was used in the MongoDB server.
- Show dbs command will show all database names and sizes of all databases.
How to Alter Database in MongoDB?
Below syntax and example show how to alter the database:
- We are using the copyDatabase command to rename a database.
Syntax:
db.copyDatabase ('name_of_old_database', 'new_database_name')
use name_of_old_database
db.dropDatabase();
- In above syntax copyDatabase is defined as the command used to copy one database to another database with a different name.
- The name of the old database is defined as the old database name, which we have used to copy the database to a new name.
- The name of the new database is defined as the new database name, which we are renaming using the copyDatabase command.
- We cannot directly rename the database; to rename the database, we are using the copyDatabase command.
- After copying to the database, we are connecting to the old database and we have to delete the old database.
The below example shows that alter database command to rename the database with a new name:
Code:
show dbs
db.copyDatabase('db_test', 'db_test1')
show dbs
use db_test
db.dropDatabase()
show dbs
Output:
- In the above example, first, we have copied the database from db_test to db_test1 database. After a successful copy from db_test to db_test1, we connected to the db_test database.
- After connecting, we dropped the old database using the dropDatabase command.
- Basically, there is no rename database command available in MongoDB, so instead of rename database command, we are using copyDatabase.
How to Delete Database in MongoDB?
Below syntax and example show how to delete the database:
- We have dropped the database in MongoDB using dropDatabase command.
The below syntax shows to drop the database, first, we need to connect the specified database which we are dropping.
Syntax:
use name_of_database
db.dropDatabase ()
- In the above syntax name of the database is defined as the database name which we are dropping from the server.
Given below example shows deleted database:
Code:
show dbs
use db_test1
db.dropDatabase ()
show dbs
Output:
- In the above example, we have deleted the database name db_test1.
- To delete the database, we need to connect to the specified database, which we are dropping in MongoDB.
- The connected database will be deleted from the database server; so after using this command, we need to check the database name which we have connected.
The below example shows the same:
Code:
show dbs
use db_test1
db.dropDatabase ()
show dbs
Output:
- In the above example, first, we have connected to the db_test database. After connecting, we have deleted the same from the database server.
Conclusion
We are creating the database by using the use db command, and if the database already exists in the database, then we are connecting to the existing database. We are renaming the database by using the copyDatabase command, also, we can drop the database by using dropDatabase command.
Recommended Articles
This is a guide to MongoDB Database. Here we discuss how to create, alter, and delete databases in MongoDB with respective query examples. You may also have a look at the following articles to learn more –