MongoDB 集合

2020-07-23  本文已影响0人  薥劃

创建集合

语法格式:

db.createCollection(name, options)

参数说明:
创建集合

> use test
switched to db test
> db.createCollection("tre")
{ "ok" : 1 }

如果要查看已有集合,可以使用 show collections 或 show tables 命令:

> show collections
tre

创建固定集合 mycol,整个集合空间大小 6142800 KB, 文档最大个数为 10000 个。

> db.createCollection("mycol", { capped : true, autoIndexId : true, size :
... 6142800, max : 10000 } )
{
"note" : "the autoIndexId option is deprecated and will be removed in a future release",
"ok" : 1
}
> show collections
mycol
tre

在 MongoDB 中,不需要创建集合。当你插入一些文档时,MongoDB 会自动创建集合。

> db.mycol2.insert({"name" : "tre"})
WriteResult({ "nInserted" : 1 })
show collections
mycol
mycol2
tre

删除集合

语法格式:

db.collection.drop()

删除集合 mycol2

> db.mycol2.drop()
true
> show collections
mycol
tre

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

上一篇 下一篇

猜你喜欢

热点阅读