Skip to content

How to Create and Drop Database in MongoDB: A Comprehensive Guide

This article will help you to create and drop database in MongoDB. In mongodb there are no command to create database. we just need to use new database

Category
Databases
Covers
MongoDB
Versions & support
About MongoDB →
On this page

MongoDB is a popular, open-source NoSQL database that provides high performance, high availability, and automatic scaling. As a document-based database, it stores data in flexible, JSON-like documents, making it easy to work with and integrate into various applications. In this article, we’ll cover the fundamentals of creating and dropping databases in MongoDB, allowing you to confidently manage your data.

Table of Contents

  1. Prerequisites
  2. Understanding MongoDB Structure
  3. Creating a MongoDB Database
  4. Listing Databases
  5. Dropping a MongoDB Database
  6. Conclusion

1. Prerequisites

Before diving into creating and dropping databases, ensure that you have MongoDB installed on your system. You can download it from the official MongoDB website. Additionally, it is helpful to have a basic understanding of JSON, JavaScript, and the command line.

2. Understanding MongoDB Structure

MongoDB organizes data into databases, collections, and documents. A database contains multiple collections, which are analogous to tables in a relational database, and each collection holds documents. Documents are BSON (Binary JSON) objects that store data in a flexible, semi-structured format.

3. Creating a MongoDB Database

In MongoDB, you don’t explicitly create a database. Instead, you create a database by switching to a non-existent database, then inserting data into it. The new database is created automatically when you insert the first document.

To create a database, follow these steps:

  1. Open the MongoDB shell by running the mongo command in your terminal or command prompt.
  2. Use the use command followed by the desired database name to switch to the new database:

  3. If the database does not exist, MongoDB will create it automatically. To verify that you’ve switched to the new database, use the db command:

  4. To insert data into the new database, create a collection and insert a document:

    Replace myCollection with the desired collection name and { key: “value” } with a valid BSON document. After executing this command, MongoDB will create the new database and collection, and store the document.

4. Listing Databases

To list all databases in your MongoDB instance, execute the show dbs command in the MongoDB shell:

This command will display a list of databases along with their sizes. The newly created database should appear in the list.

5. Dropping a MongoDB Database

To drop a database, follow these steps:

  1. Open the MongoDB shell by running the mongo command in your terminal or command prompt.
  2. Use the use command followed by the target database name to switch to the database you want to drop:

  3. To drop the current database, execute the db.dropDatabase() command:

    MongoDB will delete the specified database, including all of its collections and documents. Note that this operation is irreversible, so use caution when executing it.

Conclusion

By mastering the process of creating, listing, and dropping databases in MongoDB, you gain the foundational knowledge needed to manage your data effectively. As you continue to explore MongoDB, you’ll discover its various features and capabilities, allowing you to build powerful and scalable applications.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

4 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top