How to delete database in mongodb

How to delete database in mongodb

Introduction to MongoDB Database

MongoDB database is defined as create database for database operations, using MongoDB database we are creating the collection into it. MongoDB is not structured database so we have not used create database statement which we have used in other databases like MySQL and PostgreSQL. We can create the database in MongoDB by using the database, use keyword, it is used to create new database in MongoDB, after using the database we are creating the collection into the same. We can create multiple database on single MongoDB server default database which we have used in MongoDB is db.

How to Create Database in MongoDB?

  • Basically we have used use command to create new database in MongoDB.

Below is the syntax :

Syntax:

Use name_of_database

  • In above syntax use keyword specifies that create new database. We can create any new database by using use keyword.
  • Name of the database shows that database name which we are creating in MongoDB. We can create database by specifying the name of database parameter.
  • If using database is exist on the server it will use the existing database instead of creating the new database.

Below example shows that create new database:

Code:

use db_test
db

Output:

How to delete database in mongodb

  • In above example we have created database name as db_test after creating we can see the connected database name by using db command.
  • We can show all the created and system database by using the show dbs command.

The below example shows that list  as follows:

Code:

show dbs

Output:

How to delete database in mongodb

  • After using database it will not show in show dbs command, after creating collection or object of the database then it will show using show dbs command.

Below example shows the same:

Code:

use db_test1
show dbs
db.test.insert({"name":"ABC"})
show dbs

Output:

How to delete database in mongodb

  • In above first example we have created database name as db_test1 it will show connected database by using db command but not showing in the list of database, because we have not created any collection or object into db_test1 database.
  • In second example we have created one collection at the time of data insertion after creating collection into the db_test1 database, name of 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 MongoDB server.
  • Show dbs command will show all database name and size of all databases.

How to Alter Database in MongoDB?

Below syntax and example shows that how to alter database:

  •  We are using copyDatabase command to rename 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 other database with different name.
  • Name of old database is defined as old database name which we have used to copying database to new name.
  • Name of new database is defined as new database name which we are renaming using the copyDatabase command.
  • We cannot directly rename the database, to rename the database we are using copyDatabase command.
  • After copying to the database we are connecting to the old database and after we have deleting old database.

Below example shows that alter database command to rename the database with new name:

Code:

show dbs
db.copyDatabase('db_test', 'db_test1')
show dbs
use db_test
db.dropDatabase()
show dbs

Output:

How to delete database in mongodb

  • In above example first we have copied database from db_test to db_test1 database. After successful copy from db_test to db_test1 we have connected to db_test database.
  • After connecting we have dropped old database using dropDatabase command.
  • Basically there is no rename database command is available in MongoDB so instead of rename database command we are using copyDatabase.

How to Delete Database in MongoDB?

Below syntax and example shows that how to delete the database:

  • We have drop the database in MongoDB using dropDatabase command.

Below syntax shows to drop the database, first we need to connect the specified database which was we are dropping.

Syntax:

use name_of_database
db.dropDatabase ()

  • In above syntax name of the database is defined as database name which we are dropping from the server.
  • Drop database command is used to delete existing database.

Given below example shows delete database:

Code:

show dbs
use db_test1
db.dropDatabase ()
show dbs

Output:

How to delete database in mongodb

  • In above example we have deleted database name as db_test1.
  • To delete database we need to connect to the specified database which we are dropping in MongoDB.
  • The connected database will be deleted from database server, so after using this command we need to check the database name which we have connected.

Below example shows the same:

Code:

show dbs
use db_test1
db.dropDatabase ()
show dbs

Output:

How to delete database in mongodb

  • In 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, if the database already existed in 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.

This is a guide to MongoDB Database. Here we discuss how to create, alter, delete databases in MongoDB with respective query examples. You may also have a look at the following articles to learn more –

  1. MongoDB Timestamp
  2. MongoDB Relationships
  3. MongoDB Skip()
  4. MongoDB Features

How do I delete all databases in MongoDB?

To delete all documents in a collection, pass an empty document ({}). To limit the deletion to just one document, set to true. Omit to use the default value of false and delete all documents matching the deletion criteria.

What is Delete command MongoDB?

The delete command removes documents from a collection. A single delete command can contain multiple delete specifications. The command cannot operate on capped collections. The remove methods provided by the MongoDB drivers use this command internally.

How do I drop or delete a collection in MongoDB?

You can delete a table, or collection as it is called in MongoDB, by using the drop() method.

How do I delete a table in MongoDB?

To delete a MongoDB Collection, use db. collection. drop() command.