Python MongoDB 创建集合
MongoDB 中的集合包含一组文档,类似于关系数据库中的表。
你可以使用 创建集合() 方法。此方法接受一个表示要创建的集合名称的字符串值和一个选项(可选)参数。
使用它可以指定以下内容:
- 集合的大小。
- 上限集合中允许的最大文档数。
- 我们创建的集合是否应该是有上限的集合(固定大小的集合)。
- 我们创建的集合是否应该自动索引。
语法
以下是在 MongoDB 中创建集合的语法。
db.createCollection("CollectionName")
例子
以下方法创建一个名为 例子Collection 的集合。
> use mydb switched to db mydb > db.createCollection("例子Collection") { "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........