Facebook X (Twitter) Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook X (Twitter) Instagram
    TecAdmin
    You are at:Home»Databases»MongoDB»How to Create and Drop Database in MongoDB: A Comprehensive Guide

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

    By RahulApril 13, 20233 Mins Read

    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:

      1
      use myNewDatabase

    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:

      1
      db

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

      1
      db.myCollection.insertOne({ key: "value" })

      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:

    1
    show dbs

    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:

      1
      use myExistingDatabase

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

      1
      db.dropDatabase()

      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.

    database drop mongodb
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How to Calculate the MySQL Database Size

    Changing the Default MySQL Data Directory on CentOS & RHEL

    PowerShell Script to Backup SQL Server Databases

    View 4 Comments

    4 Comments

    1. Ashwini on October 20, 2020 5:28 am

      Which software we should use to execute mongodb for Android

      Reply
    2. ASHUTOSH MORE on August 16, 2019 4:27 pm

      All MONGODB QUERIES SHOULD BE PROPERLY GIVEN ALL THE TIME .HELP STUDENTS.

      Reply
    3. Ferrero on April 17, 2018 5:16 pm

      Can you tell me where do I have to write those comands please?

      Reply
      • Rahul K. on April 18, 2018 4:10 am

        Use mongo shell https://tecadmin.net/tutorial/mongodb/mongo-shell/

        Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to Create and Use Custom Python Module
    • How to Install and Use Podman on Ubuntu 22.04 & 20.04
    • Setting Up Laravel with Docker and Docker-compose
    • Setting Up Development Environments with PHP and Docker
    • Using Composer with Different PHP Versions in Linux
    Facebook X (Twitter) Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.