On this page本页内容
db.
createCollection
(name, options)¶Creates a new collection or view. 创建新的集合或视图。For views, see also 有关视图,请参阅db.createView()
.db.createView()
。
Because MongoDB creates a collection implicitly when the collection is first referenced in a command, this method is used primarily for creating new collections that use specific options. 由于MongoDB在命令中首次引用集合时隐式创建集合,因此此方法主要用于创建使用特定选项的新集合。For example, you use 例如,您可以使用db.createCollection()
to create a capped collection, or to create a new collection that uses document validation.db.createCollection()
创建一个封顶集合,或者创建一个使用文档验证的新集合。
db.createCollection()
is a wrapper around the database command create
.createCollection()
是数据库命令create
的包装器。
The db.createCollection()
method has the following prototype form:db.createCollection()
方法具有以下原型形式:
Starting in MongoDB 4.2
MongoDB removes the MMAPv1 storage engine and the MMAPv1 specific options MongoDB针对paddingFactor
, paddingBytes
, preservePadding
for db.createCollection()
.db.createCollection()
删除了MMAPv1存储引擎和特定于MMAPv1的选项paddingFactor
、paddingBytes
和preservePadding
。
The db.createCollection()
method has the following parameters:db.createCollection()
方法具有以下参数:
name |
string | |
options |
document |
The options
document contains the following fields:options
文档包含以下字段:
capped |
boolean | true . true 。true , you must also set a maximum size in the size field.true ,则还必须在size 字段中设置最大大小。 | ||||||||
autoIndexId |
boolean |
Important
| ||||||||
size |
number | size field is required for capped collections and ignored for other collections.size 字段对于封顶集合是必需的,而对于其他集合则被忽略。 | ||||||||
max |
number | size limit takes precedence over this limit. size 限制优先于此限制。size limit before it reaches the maximum number of documents, MongoDB removes old documents. size 限制,MongoDB将删除旧文档。max limit, ensure that the size limit, which is required for a capped collection, is sufficient to contain the maximum number of documents.max 限制,请确保size 限制(上限集合所需)足以包含最大数量的文档。 | ||||||||
storageEngine |
document |
| ||||||||
validator |
document |
Note
| ||||||||
validationLevel |
string |
| ||||||||
validationAction |
string |
Important
| ||||||||
indexOptionDefaults |
document |
| ||||||||
viewOn |
string |
| ||||||||
pipeline |
array |
See also
| ||||||||
collation |
document |
| ||||||||
writeConcern |
document |
|
If the deployment enforces authentication/authorization, 如果部署强制进行身份验证/授权,db.createCollection()
requires the following privileges:db.createCollection()
需要以下权限:
| |
| |
|
A user with the 在数据库上具有readWrite
built in role on the database has the required privileges to run the listed operations. readWrite
内置角色的用户具有运行列出的操作所需的权限。Either create a user with the required role or grant the role to an existing user.用户或将该角色授予现有用户。
Changed in version 4.2.在版本4.2中更改。
db.createCollection()
obtains an exclusive lock on the specified collection or view for the duration of the operation. 在操作期间获取指定集合或视图的独占锁。All subsequent operations on the collection must wait until 集合上的所有后续操作都必须等待db.createCollection()
releases the lock. db.createCollection()
释放锁。db.createCollection()
typically holds this lock for a short time.通常会在短时间内保持此锁。
Creating a view requires obtaining an additional exclusive lock on the 创建视图需要在数据库中的system.views
collection in the database. system.views
集合上获得额外的独占锁。This lock blocks creation or modification of views in the database until the command completes.此锁将阻止在数据库中创建或修改视图,直到命令完成。
Prior to MongoDB 4.2, 在MongoDB 4.2之前,db.createCollection()
obtained an exclusive lock on the parent database, blocking all operations on the database and all its collections until the operation completed.db.createCollection()
获得了父数据库的独占锁,阻止了数据库及其所有集合上的所有操作,直到操作完成。
Changed in version 4.4.在版本4.4中更改。
Starting in MongoDB 4.4 with feature compatibility version (fcv) 从功能兼容版本(fcv)“4.4”的MongoDB 4.4开始,如果事务不是跨切分写入事务,则可以在多文档事务中创建集合和索引。"4.4"
, you can create collections and indexes inside a multi-document transaction if the transaction is not a cross-shard write transaction.
To use 要在事务中使用db.createCollection()
in a transaction, the transaction must use read concern "local"
. db.createCollection()
,该事务必须使用读关注点"local"
。If you specify a read concern level other than 如果指定的读取关注级别不是"local"
, the transaction fails."local"
,则事务失败。
Capped collections have maximum size or document counts that prevent them from growing beyond maximum thresholds. 封顶集合具有最大大小或文档计数,可防止其增长超过最大阈值。All capped collections must specify a maximum size and may also specify a maximum document count. 所有封顶集合必须指定最大大小,还可以指定最大文档数。MongoDB removes older documents if a collection reaches the maximum size limit before it reaches the maximum document count. Consider the following example:如果集合在达到最大文档数之前达到最大大小限制,MongoDB将删除旧文档。考虑下面的例子:
This command creates a collection named 此命令创建一个名为log
with a maximum size of 5 megabytes and a maximum of 5000 documents.log
的集合,最大大小为5兆字节,最大文档数为5000个。
See Capped Collections for more information about capped collections.有关封顶集合的详细信息,请参阅封顶集合。
New in version 3.2.版本3.2中的新功能。
Collections with validation compare each inserted or updated document against the criteria specified in the 具有验证的集合将每个插入或更新的文档与validator
option. validator
选项中指定的条件进行比较。Depending on the 根据validationLevel
and validationAction
, MongoDB either returns a warning, or refuses to insert or update the document if it fails to meet the specified criteria.validationLevel
和validationAction
,MongoDB要么返回警告,要么拒绝插入或更新未满足指定条件的文档。
The following example creates a 以下示例使用JSON模式验证程序创建contacts
collection with a JSON Schema validator:contacts
集合:
Note
MongoDB 3.6 adds the MongoDB 3.6添加了$jsonSchema
operator to support JSON Schema validation.$jsonSchema
运算符以支持JSON模式验证。
注:要查看上述集合的架构验证,请使用以下命令:db.getCollectionInfos({name:'contacts'})[0].options.validator.$jsonSchema
With the validator in place, the following insert operation fails validation:验证程序就位后,以下插入操作无法通过验证:
The method returns the error in the 该方法在WriteResult
:WriteResult
中返回错误:
For more information, see Schema Validation. 有关更多信息,请参阅架构验证。To view the validation specifications for a collection, use the 要查看集合的验证规范,请使用db.getCollectionInfos()
method.db.getCollectionInfos()
方法。
New in version 3.4.版本3.4中的新功能。
Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks.排序规则允许用户为字符串比较指定特定于语言的规则,例如字母大小写和重音符号的规则。
You can specify collation at the collection or view level. 可以在集合或视图级别指定排序规则。For example, the following operation creates a collection, specifying a collation for the collection (See Collation Document for descriptions of the collation fields):例如,以下操作创建集合,为集合指定排序规则(有关排序规则字段的说明,请参阅排序规则文档):
This collation will be used by indexes and operations that support collation unless they explicitly specify a different collation. 支持排序规则的索引和操作将使用此排序规则,除非它们显式指定不同的排序规则。For example, insert the following documents into 例如,将以下文档插入myColl
:myColl
:
The following operation uses the collection’s collation:以下操作使用集合的排序规则:
The operation returns documents in the following order:该操作按以下顺序返回文档:
The same operation on a collection that uses simple binary collation (i.e. no specific collation set) returns documents in the following order:对使用简单二进制排序规则(即无特定排序规则集)的集合执行的相同操作将按以下顺序返回文档:
You can specify collection-specific storage engine configuration options when you create a collection with 使用db.createCollection()
. db.createCollection()
创建集合时,可以指定特定于集合的存储引擎配置选项。Consider the following operation:考虑以下操作:
This operation creates a new collection named 此操作将创建一个名为users
with a specific configuration string that MongoDB will pass to the wiredTiger
storage engine. users
的新集合,其中包含MongoDB将传递给wiredTiger
存储引擎的特定配置字符串。See the WiredTiger documentation of collection level options for specific 有关特定的WiredTiger选项,请参阅集合级别选项的wiredTiger
options.WiredTiger
文档。