当前位置:  首页>> 技术小册>> MongoDB入门教程

本章节我们为大家介绍如何使用 MongoDB 来删除集合。

MongoDB 中使用 drop() 方法来删除集合。

语法格式:

  1. db.collection.drop()

参数说明:


返回值

如果成功删除选定集合,则 drop() 方法返回 true,否则返回 false。

实例
在数据库 mydb 中,我们可以先通过 show collections 命令查看已存在的集合:

  1. >use mydb
  2. switched to db mydb
  3. >show collections
  4. mycol
  5. mycol2
  6. system.indexes
  7. maxiaoke
  8. >

接着删除集合 mycol2 :

  1. >db.mycol2.drop()
  2. true
  3. >

通过 show collections 再次查看数据库 mydb 中的集合:

  1. >show collections
  2. mycol
  3. system.indexes
  4. maxiaoke
  5. >

从结果中可以看出 mycol2 集合已被删除。


该分类下的相关小册推荐: