
- Python MongoDB 教程
- Python MongoDB - 主頁
- Python MongoDB - 簡介
- Python MongoDB - 建立資料庫
- Python MongoDB - 建立集合
- Python MongoDB - 插入文件
- Python MongoDB - 查詢
- Python MongoDB - 查詢
- Python MongoDB - 排序
- Python MongoDB - 刪除文件
- Python MongoDB - 刪除集合
- Python MongoDB - 更新
- Python MongoDB - 限制
- Python MongoDB 有用資源
- Python MongoDB - 快速指南
- Python MongoDB - 有用資源
- Python MongoDB - 討論
Python MongoDB - 建立集合
MongoDB 中的集合儲存了一組文件,它類似於關係資料庫中的表。
你可以使用 createCollection() 方法建立一個集合。此方法接受一個代表要建立的集合名稱的字串值,以及一個選項(可選)引數。
使用此方法你可以指定以下內容 −
集合的 大小。
受限集合中允許的文件的 最大 數量。
我們建立的集合是否應該是受限集合(固定大小的集合)。
我們建立的集合是否應該是自動編制的索引。
語法
以下是 MongoDB 中建立集合的語法。
db.createCollection("CollectionName")
示例
下列方法建立名為 ExampleCollection 的集合。
> use mydb switched to db mydb > db.createCollection("ExampleCollection") { "ok" : 1 } >
類似地,以下是使用 createCollection() 方法的選項建立集合的查詢。
>db.createCollection("mycol", { capped : true, autoIndexId : true, size : 6142800, max : 10000 } ) { "ok" : 1 } >
使用 Python 建立集合
以下 python 示例連線到 MongoDB 中的一個數據庫 (mydb),並在此資料庫中建立一個集合。
示例
from pymongo import MongoClient #Creating a pymongo client client = MongoClient('localhost', 27017) #Getting the database instance db = client['mydb'] #Creating a collection collection = db['example'] print("Collection created........")
輸出
Collection created........
廣告