MongoDB之常用命令初探
2017-06-28 本文已影响0人
Rhine_he
1、show dbs显示当前所有数据库

2、db命令查看当前所在数据库

可以看出,在未创建数据库的情况下查询当前所在的数据库,为test。
原因:MongoDB 中默认的数据库为 test,如果你没有创建新的数据库,集合将存放在 test 数据库中。
3、use命令创建数据库test,创建成功后查看当前所在数据库为test。
- 注:若use dbName,此dbName是一个已经存在的数据库,那么 use 的作用是切换到这个数据库,否则,use 的作用是创建新的数据库。
- 由下图可以看出,刚创建的数据库 test并不在数据库的列表中, 要显示它,我们需要向 其插入一些数据。
原因:use命令创建的数据库并不会立马给其数据库分配空间,而是需要通过 insert 给该数据库插入一条数据之后才会创建该数据库。

4.给test数据库插入 insert 一条数据

5.删除数据库db.dropDatabase()
use进入到需要删除的数据库;删除该数据库;查看当前所有数据库,可见该数据库已被删除
