MongoDB is the most popular NoSQL database among enterprises and startups, and its high scalability makes it ideal for contemporary web-apps that need to scale as their user base grows. MongoDB differs from standard relational databases in that it stores data in json-like objects rather than tables as in relational databases.

In this post, we will learn how to use the MongoDB Drop Database command to delete a MongoDB database. Dropping a database implies removing all of its collections (tables in MongoDB) and indexes. Individual collections can be deleted if you do not wish to delete the entire database.

Dropping a database is straightforward and may be accomplished via three methods from the command line as well as any GUI (Graphical User Interface) tool that connects to a running MongoDB server.

We primarily use db.dropDatabase to destroy a MongoDB database from the command line (). First, we'll figure out how to do it.

Definition of Drop Database

As previously stated, the db.dropDatabase() command is used to drop or remove a MongoDB database.

db.dropDatabase(<writeConcern>)

This deletes the current database, as well as all collections and indexes within it.

The optional argument writeConcern is a document specifying the write concern to use if it is greater than majority. The following fields can be included in a write concern:

{ w: <value>, j: <boolean>, wtimeout: <number> }

The w option is used to request that the write operation be acknowledged by a specified number of mongod instances.
The j option is used to request confirmation that the write operation was successful.
journal
The wtimeout option specifies a time limit to prevent write operations from blocking forever.
The following options are available when using the w option:

w: 1 - Requests confirmation that the write concern has been routed to the single mongod instance. It's also the default choice.
w: 0 - No acknowledgement of the write operation is requested.
w: "majority" - It requires confirmation that the write operation was successfully completed on the majority of primary and secondary servers.

We shall now investigate the j option values.

j: true - It requires confirmation that the various mongod instances listed in w: value> have been written to the on-disk journal.
The wtimeout option provides the time limit for the write concern in milliseconds. It causes write operations to fail after the set time limit.

dropDatabase

We may also use the dropDatabase command to destroy the current MongoDB database. The command is written as follows.

{ dropDatabase: 1, writeConcern: <document>, comment: <any> }

The optional fields for this command are as follows:

The optional argument writeConcern is a document specifying the write concern to use if it is greater than majority. We just looked into it thoroughly earlier.
comment is a user-supplied comment associated to this command. This will appear in the logs once set.

Dropping data with the dropDatabase command

We can delete the database using the dropDatabase command. I'm using the mongo command to connect to my local MongoDB instance from the Windows terminal.

> mongo

Following that, we will use the show dbs command to display all of the databases in my MongoDB database. It will display all of our databases. I simply used it to generate the example database.

> show dbs
admin ? ?         0.000GB
config ?          0.000GB
codesolutionstuff 0.000GB
local ? ?         0.000GB

To drop the example database, we must first enter it using the use example command.

> use codesolutionstuff 
switched to db codesolutionstuff 

We will now use the dropDatabase command from within a database. runCommand(). The runCommand function now executes the command in the context of the current database. This implies it will execute the dropDatabase command in the current database, as shown in the following example:

> db.runCommand( { dropDatabase: 1 } )
{ "dropped" : "codesolutionstuff ", "ok" : 1 }

As previously stated, the dropDatabase command accepts an optional writeConcern argument as well as a comment field. The syntax is then written as follows.

{ dropDatabase: 1, writeConcern: <document>, comment: <any> }

dropDatabase() Method Dropping

We may also delete the database using the db.dropDatabase() method. It also deletes the current database and any associated files.

Connect to the mongoDb database once more with the command mongo. After that, we can use the show dbs command to inspect all of the databases.

For the db.dropDatabase() method, we must also enter the database that we are dropping by using the dbName> command.

Now, in our example, we will use the db.dropDatabase() command to delete the database.

> db.dropDatabase()
{ "dropped" : "example", "ok" : 1 }
The db.dropDatabase() method also accepts an optional writeConcern argument. The syntax then becomes:
{ w: <value>, j: <boolean>, wtimeout: <number> }

Conclusion

As a result, we've seen how simple it is to create and drop databases in MongoDB. The MongoDB drop all database command deletes all data associated with the destroyed database.


Recommended Posts

View All

10 Reasons to Study MongoDB in 2023


Why should we pick MongoDB, we'll discuss here. It has advantages and disadvantages of its own, just like every coin has two sides.

How to Use MongoDB Limit Records


MongoDB Limit Records, Use MongoDB Limit Records, Limit Documents, Query Limitation, MongoDB Database Management.

CRUD Operations in MongoDB


The conventions of a user interface that allow users to view, search, and alter portions of the database are referred to as CRUD operations MongoDB.

Defining, Creating and Dropping a MongoDB collection


MongoDB Create Collection, MongoDB Drop Collection, how to create and drop a collection in MongoDB, an example of a collection in MongoDB, MongoDB col...

Create, Check, and Uncap the Capped Collection in MongoDB


MongoDB Capped Collection tutorial- how to create, convert, check, advantages, disadvantages of cap collection, How to uncapped the capped collection,...