建立一個集合
首先選擇或建立資料庫。
> use mydb
switched to db mydb
使用 db.createCollection("yourCollectionName") 方法,你可以顯式建立 Collection。
> db.createCollection("newCollection1")
{ "ok" : 1 }
使用 show collections 命令檢視資料庫中的所有集合。
> show collections
newCollection1
system.indexes
> 
db.createCollection() 方法具有以下引數:
| 引數 | 型別 | 描述 | 
|---|---|---|
| 名稱 | 字串 | 要建立的集合的名稱。 | 
| 選項 | 文獻 | *可選的。*用於建立上限集合或用於在新集合中預分配空間的配置選項。 | 
flfl 示例顯示了 createCollection() 方法的語法,幾個重要選項
>db.createCollection("newCollection4", {capped :true, autoIndexId : true, size : 6142800, max : 10000})
{ "ok" : 1 }
db.collection.insert() 和 db.collection.createIndex() 操作都會建立它們各自的集合(如果它們尚不存在)。
> db.newCollection2.insert({name : "XXX"})
> db.newCollection3.createIndex({accountNo : 1})
現在,使用 show collections 命令顯示所有集合
> show collections
newCollection1
newCollection2
newCollection3
newCollection4
system.indexes
如果要檢視插入的文件,請使用 find() 命令。
> db.newCollection2.find()
{ "_id" : ObjectId("58f26876cabafaeb509e9c1f"), "name" : "XXX" }