MongoDB删除数据库和删除集合

小虾米 阅读:206 2022-06-14 19:20:51 评论:0
一 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:
  1. > show dbs
  2. local 0.000GB
  3. runoob 0.000GB
  4. students 0.000GB
  5. test 0.000GB
  6. > use runoob
  7. switched to db runoob
  8. > show tables
  9. runoobtable
  10. > db.runoobtable.drop()
  11. true
  12. > show dbs
  13. local 0.000GB
  14. students 0.000GB
  15. test 0.000GB

标签:mongodb
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

搜索
关注我们

一个IT知识分享的公众号