創建集合:
db.createCollection(name, options)
- options 可以是如下參數:
-
capped(布爾):(可選)如果爲 true,則創建固定集合。固定集合是指有着固定大小的集合,當達到最大值時,它會自動覆蓋最早的文檔。 **當該值爲 true 時,必須指定 size 參數。
-
autoIndexId(布爾):(可選)如爲 true,自動在 _id 字段創建索引。默認爲 false。
-
size(數值):可選)爲固定集合指定一個最大值,以千字節計(KB)。 **如果 capped 爲 true,也需要指定該字段。
-
max(數值):(可選)指定固定集合中包含文檔的最大數量。
-
在插入文檔時,MongoDB 首先檢查固定集合的 size 字段,然後檢查 max 字段。
> use demo
switched to db demo
> db.createCollection("demoList")
{ "ok" : 1 }
查看集合:
show collections 或 show tables
> show collections
demoList
> show tables
demoList
>
插入文檔時直接創建集合
> db.demoList2.insert({"name":"測試集合2"})
WriteResult({ "nInserted" : 1 })
> show tables
demoList
demoList2
>
刪除集合:
db.collection.drop()
> show tables
demoList
demoList2
> db.demoList.drop()
true
> show collections
demoList2
>