MongoDB删除数据库和删除集合

一 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

猜你喜欢

转载自cakin24.iteye.com/blog/2387002