乐闻世界logo
搜索文章和话题

How do I drop a MongoDB database from the command line?

1个答案

1

Deleting a database in MongoDB can be achieved through various methods, with the most common approach being via the MongoDB command line interface. Below are the step-by-step instructions for deleting a MongoDB database using the command line:

  1. Open the command line interface:
    Launch the terminal or command prompt.

  2. Connect to the MongoDB instance:
    If MongoDB is running locally, connect using the following command:

    bash
    mongo

    For remote servers or specific ports, use:

    bash
    mongo --host <hostname or IP> --port <port number>
  3. Select the target database:
    Before deletion, select the database using the use command:

    bash
    use <database name>
  4. Delete the database:
    Execute the db.dropDatabase() command to remove the selected database and all its data:

    bash
    db.dropDatabase()

For example, to delete a database named testdb, follow these commands:

bash
mongo # Connect to MongoDB use testdb # Select the testdb database db.dropDatabase() # Delete the database

Always back up any critical data before executing deletion operations, as the db.dropDatabase() command permanently removes data with no recovery option. Exercise caution when performing such operations.

2024年7月18日 01:29 回复

你的答案