版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014453898/article/details/84060000
(一)创建数据库
创建数据库“textCollection”:
> use textBase
switched to db textBase
查看当前数据库的名字:
>db
textBase
>
显示所有数据库:
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
可以看到,新创建的textCollection数据库并没显示,原因是要往先创建的数据库中插入数据才能够让其显示:
> db.textBase.insert({"name":"xiaoming"})
WriteResult({ "nInserted" : 1 })
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
textBase 0.000GB
如上图所示,往textBase中插入了{”name“:”xiaoming“}后,再show dbs,就能显示textBase了。
(二)删除数据库
语法:进入某个数据后后,调用db.dropDatabase()。
> use textBase
switched to db textBase
> db.dropDatabase()
{ "dropped" : "textBase", "ok" : 1 }
>
{ "dropped" : "textCollection", "ok" : 1 }表示删除成功。
(三)创建集合(collection)
集合就是mysql里面的数据表(table)。mongodb中利用createCollection()方法来创建集合。
db.createColleciton(name,option)
其中参数name表示要创建集合的名字,option为可选项,决定创建集合的某些属性,可以不填。
往集合中插入文档(文档即每一条的数据)时,mongodb会先检查固定集合中的size字段,然后再检查max字段。
size字段决定这个集合一共有多少KB,而max字段表示这个集合最多能有多少个文档。
应用实例:
(1)一般的用法,并无option。进入test数据库后,创建了名为“textCollection”的集合:
> use test
switched to db test
> db.createCollection("testCollection")
{ "ok" : 1 }
>
{"ok":1}是创建集合成功的标志。
(2)带有option的创建集合,这个集合最大为4096KB,最多可存储100条文档:
> db.createCollection("textCollection", { capped : true, autoIndexId : true, size :
4096, max : 100} )
{ "ok" : 1 }
>
(四)删除集合
先查看已有集合:show collections 和 show tables都行:
> show collections
textCollection
删除名为”textCollection“的集合:
> db.textCollection.drop()
true
>