On this page本页内容
create
Explicitly creates a collection or view.显式创建集合或视图。
create
has the following form:具有以下形式:
{
create: <collection or view name>,
capped: <true|false>,
timeseries: {
timeField: <string>,
metaField: <string>,
granularity: <string>
},
expireAfterSeconds: <number>,
autoIndexId: <true|false>,
size: <max_size>,
max: <max_documents>,
storageEngine: <document>,
validator: <document>,
validationLevel: <string>,
validationAction: <string>,
indexOptionDefaults: <document>,
viewOn: <source>,
pipeline: <pipeline>,
collation: <document>,
writeConcern: <document>,
comment: <any>
}
create
has the following fields:具有以下字段:
create | string | |||||||||
capped | boolean | true . true 。true , you must also set a maximum size in the size field.true ,则还必须在size 字段中设置最大大小。
| ||||||||
timeseries.timeField | string | timeField .timeField 的值。
| ||||||||
timeseries.metaField | string |
| ||||||||
timeseries.granularity | string | "seconds" (default), "minutes" , and "hours" . "seconds" (默认值)、"minutes" 和"hours" 。granularity parameter accurately improves performance by optimizing how data in the time series collection is stored internally.granularity 参数可以准确地提高性能。 | ||||||||
expireAfterSeconds | number | |||||||||
autoIndexId | boolean |
Deprecated since version 3.2. | ||||||||
size | integer | size field is required for capped collections and ignored for other collections.size 字段对于有上限的集合是必需的,而对于其他集合则被忽略。
| ||||||||
max | integer | 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 |
{ <storage-engine-name>: <options> }
| ||||||||
validator | document |
| ||||||||
validationLevel | string |
| ||||||||
validationAction | string |
| ||||||||
indexOptionDefaults | document |
{ <storage-engine-name>: <options> }
| ||||||||
viewOn | string |
| ||||||||
pipeline | array |
| ||||||||
collation |
collation: { locale: <string>, caseLevel: <boolean>, caseFirst: <string>, strength: <int>, numericOrdering: <boolean>, alternate: <string>, maxVariable: <string>, backwards: <boolean> }
| |||||||||
writeConcern | document |
| ||||||||
comment | any |
|
The db.createCollection()
method and the db.createView()
method wrap the create
command.db.createCollection()
方法和db.createView()
方法包装create
命令。
Changed in version 4.2.在版本4.2中更改。
create
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 对集合的所有后续操作都必须等到create
releases the lock. create
释放锁。create
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之前,create
obtained an exclusive lock on the parent database, blocking all operations on the database andall its collections until the operation completed.create
获得了父数据库的独占锁,在操作完成之前阻止对数据库及其所有集合的所有操作。
Changed in version 4.4.在版本4.4中更改。
Starting in MongoDB 4.4, you can create collections and indexes inside a multi-document transaction if the transaction is not a cross-shard write transaction.从MongoDB 4.4开始,如果事务不是跨分片写入事务,则可以在多文档事务中创建集合和索引。
To use 要在事务中使用create
in a transaction, the transaction must use read concern "local"
. create
,事务必须使用读取关注点"local"
。If you specify a read concern level other than 如果指定的读取关注级别不是"local"
, the transaction fails."local"
,则事务将失败。
Changed in version 5.0.在版本5.0中更改。
When using Stable API V1, you cannot specify the following fields in a 使用Stable API V1时,不能在create
command:create
命令中指定以下字段:
autoIndexId
capped
indexOptionDefaults
max
size
storageEngine
If the deployment enforces authentication/authorization, 如果部署强制执行身份验证/授权,则create
requires the following privileges:create
需要以下权限:
| |
| |
|
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.创建具有所需角色的用户,或将该角色授予现有用户。
To create a capped collection limited to 64 kilobytes, issue the command in the following form:要创建限制为64 KB的封顶集合,请按以下格式发出命令:
db.runCommand( { create: "collection", capped: true, size: 64 * 1024 } )
To create a time series collection that captures weather data for the past 24 hours, issue this command:要创建捕获过去24小时天气数据的时间序列集合,请发出以下命令:
db.createCollection( "weather24h", { timeseries: { timeField: "timestamp", metaField: "data", granularity: "hours" }, expireAfterSeconds: 86400 } )
In this example 在此示例中,expireAfterSeconds
is specified as 86400
which means documents expire 86400
seconds after the timestamp
value. expireAfterSeconds
指定为86400
,这意味着文档在timestamp
值之后86400
秒过期。See Set up Automatic Removal for Time Series Collections (TTL).请参见设置时间序列集合(TTL)的自动删除。
Changed in version 4.2.在版本4.2中更改。
The view definition 视图定义pipeline
cannot include the $out
or the $merge
stage. pipeline
不能包含$out
或$merge
阶段。If the view definition includes nested pipeline (e.g. the view definition includes 如果视图定义包含嵌套管道(例如,视图定义包含$lookup
or $facet
stage), this restriction applies to the nested pipelines as well.$lookup
或$facet
阶段),则此限制也适用于嵌套管道。
To create a view using the 要使用create
command, use the following syntax:create
命令创建视图,请使用以下语法:
db.runCommand( { create: <view>, viewOn: <source>, pipeline: <pipeline> } )
or if specifying a collation:或者如果指定排序规则:
db.runCommand( { create: <view>, viewOn: <source>, pipeline: <pipeline>, collation: <collation> } )
For example, given a collection 例如,给定一份包含以下文档的集合调survey
with the following documents:survey
:
{ _id: 1, empNumber: "abc123", feedback: { management: 3, environment: 3 }, department: "A" } { _id: 2, empNumber: "xyz987", feedback: { management: 2, environment: 3 }, department: "B" } { _id: 3, empNumber: "ijk555", feedback: { management: 3, environment: 4 }, department: "A" }
The following operation creates a 以下操作使用managementRatings
view with the _id
, feedback.management
, and department
fields:_id
、feedback.management
和department
字段创建managementRatings
视图:
db.runCommand ( { create: "managementFeedback", viewOn: "survey", pipeline: [ { $project: { "management": "$feedback.management", department: 1 } } ] } )
The view definition is public; i.e. 视图定义是公共的;亦即视图上的db.getCollectionInfos()
and explain
operations on the view will include the pipeline that defines the view. db.getCollectionInfos()
和explain
操作将包括定义视图的管道。As such, avoid referring directly to sensitive fields and values in view definitions.因此,避免直接引用视图定义中的敏感字段和值。
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):例如,以下操作创建集合,为集合指定排序规则(有关排序规则字段的说明,请参阅排序规则文档):
db.runCommand ( { create: "myColl", collation: { locale: "fr" } });
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
中插入以下文档:
{ _id: 1, category: "café" } { _id: 2, category: "cafe" } { _id: 3, category: "cafE" }
The following operation uses the collection's collation:以下操作使用集合的排序规则:
db.myColl.find().sort( { category: 1 } )
The operation returns documents in the following order:该操作按以下顺序返回文档:
{ "_id" : 2, "category" : "cafe" } { "_id" : 3, "category" : "cafE" } { "_id" : 1, "category" : "café" }
The same operation on a collection that uses simple binary collation (i.e. no specific collation set) returns documents in the following order:对使用简单二进制排序规则(即没有特定的排序规则集)的集合执行相同的操作将按以下顺序返回文档:
{ "_id" : 3, "category" : "cafE" } { "_id" : 2, "category" : "cafe" } { "_id" : 1, "category" : "café" }
You can specify collection-specific storage engine configuration options when you create a collection with 使用db.createCollection()
. db.createCollection()
创建集合时,可以指定特定于集合的存储引擎配置选项。Consider the following operation:考虑以下操作:
db.runCommand( { create: "users", storageEngine: { wiredTiger: { configString: "<option>=<setting>" } } } )
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
options.WiredTiger
选项,请参阅WiredTiger集合级选项文档。