Create & Drop Database
Share:
MongoDB is a NoSQL database that provides high performance, high availability, and easy scalability. It works on the concept of collections and documents, using a document-oriented data model. Creating and dropping databases in MongoDB is a routine task for developers and administrators, a crucial part for any effective database management process. In this tutorial, we're going to learn how to create and drop databases in MongoDB. We'll be using MongoDB shell and a JavaScript-based command-line interface.
Let's get started with the process of creating a database first.
Creating a Database in MongoDB
Creating a database in MongoDB is quite straightforward. You use the "use" command followed by the name of the database you want to create. The command is designed such that if the specified database does not exist, it creates one for you. Here is the basic syntax for creating a database:
use DATABASE_NAME
If you want to create a database named "testDB", the command would look like this:
use testDB
The MongoDB shell will respond with the following output:
switched to db testDB
Interestingly, this command alone does not physically create a database. MongoDB will create the database only after you've inserted some data into it. To insert some data, you need to create a collection and then add a document to this collection. Here is an example:
db.createCollection('testCollection')
db.testCollection.insert({"name":"John", "age":30, "city":"New York"})
In the first line, we're using the createCollection
command to create a collection named "testCollection". In the second line, we're inserting a simple document containing some dummy data. Now, if you list the databases using the show dbs
command, you'll see your newly-created database "testDB" in the list.
If you want to verify the current database you are connected to, use the db
command.
db
This returns the name of the database you are currently using. If it returns "testDB", you're connected to your newly created database.
Drop Database in MongoDB
Dropping a database in MongoDB is as easy as creating one. But beware, dropping a database will permanently delete the database, all its collections and documents, and cannot be undone. So always double-check before you issue the drop database command.
The command to be used to drop a database is db.dropDatabase()
. Here is how to use it:
use testDB
db.dropDatabase()
In the first line, we switch to the database named "testDB". In the second line, we issue the db.dropDatabase()
command, which drops the currently active database.
The MongoDB shell will respond with the following output:
{ "dropped" : "testDB", "ok" : 1 }
This means that the database "testDB" has been successfully dropped. If you issue the show dbs
command now, you won't see "testDB" in the list anymore. Before using the db.dropDatabase()
command, always ensure you're connected to the correct database that you want to drop.
Algorithmically, it follows the below steps:
- Use the 'use DATABASE_NAME' command to switch to the database you want to drop.
- Run db.dropDatabase() command in the MongoDB shell.
Conclusion
In this tutorial, you've learned how to create and drop databases in MongoDB. Remember the use
command creates a new database only if it doesn't exist already and only creates it physically once data is inserted. The db.dropDatabase()
command, on the other hand, permanently deletes the database. Always be sure to verify which database is currently in use with the db
command before deleting any.
The ability to create and remove databases is fundamental to using MongoDB effectively. Whether you're creating a new project, testing different databases, or managing existing databases – these operations will often come into play. Make sure you have a good understanding of these commands and their implications on your overall data. Keep practicing different operations in MongoDB to improve your efficiency and skills with this versatile NoSQL database system.
0 Comment
Sign up or Log in to leave a comment