---
title: "MongoDB删除数据库和删除集合"
author: "cakin24"
date: "2017-07-25T21:53:28Z"
tags: ["MongoDB", "删除", "数据库", "集合"]
categories: ["编程语言"]
slug: "mongodb"
draft: false
lastmod: "2017-07-25T21:53:28Z"
id: 2386835
url: "https://cakin24.iteye.com/blog/2386835"
---

**一 MongoDB删除数据库**

1、MongoDB 删除数据库的语法格式如下：

db.dropDatabase()

删除当前数据库，默认为 test，你可以使用 db 命令查看当前数据库名。

2、实例

以下实例我们删除了数据库 students。

首先，查看所有数据库：

1. `> show dbs`
2. `local 0.000GB`
3. `students 0.000GB`
4. `test 0.000GB`

接下来我们切换到数据库 students：

1. `> use students`
2. `switched to db students`

执行删除命令：

1. `> db.dropDatabase()`
2. `{ "dropped" : "students", "ok" : 1 }`

最后，我们再通过 show dbs 命令数据库是否删除成功：

1. `> show dbs`
2. `local 0.000GB`
3. `test 0.000GB`

 

**二 删除集合**

1、集合删除语法格式如下：

db.collection.drop()

2、实例

以下实例删除了 runoob 数据库中的集合 runoobtable：

01. `> show dbs`
02. `local 0.000GB`
03. `runoob 0.000GB`
04. `students 0.000GB`
05. `test 0.000GB`
06. `> use runoob`
07. `switched to db runoob`
08. `> show tables`
09. `runoobtable`
10. `> db.runoobtable.drop()`
11. `true`
12. `> show dbs`
13. `local 0.000GB`
14. `students 0.000GB`
15. `test 0.000GB`