On this page本页内容
create
¶Explicitly creates a collection or view.
Note
The view created by this command does not refer to materialized views. For discussion of on-demand materialized views, see $merge
instead.
create
has the following form:
Starting in MongoDB 4.2
MongoDB removes the MMAPv1 storage engine and the MMAPv1 specific option flags
for create
.
create
has the following fields:
create |
string | The name of the new collection or view. See Naming Restrictions. | ||||||||
capped |
boolean | true . If you specify true , you must also set a maximum size in the size field. | ||||||||
autoIndexId |
boolean |
Important Starting in MongoDB 4.0, you cannot set the option Deprecated since version 3.2. | ||||||||
size |
integer | size field is required for capped collections and ignored for other collections. | ||||||||
max |
integer | size limit takes precedence over this limit. If a capped collection reaches the size limit before it reaches the maximum number of documents, MongoDB removes old documents. If you prefer to use the max limit, ensure that the size limit, which is required for a capped collection, is sufficient to contain the maximum number of documents. | ||||||||
storageEngine |
document |
Allows users to specify configuration to the storage engine on a per-collection basis when creating a collection. The value of the Storage engine configuration specified when creating collections are validated and logged to the oplog during replication to support replica sets with members that use different storage engines. | ||||||||
validator |
document |
The Note
| ||||||||
validationLevel |
string |
| ||||||||
validationAction |
string |
Important Validation of documents only applies to those documents as determined by the
| ||||||||
indexOptionDefaults |
document |
The Storage engine configuration specified when creating indexes are validated and logged to the oplog during replication to support replica sets with members that use different storage engines.
| ||||||||
viewOn |
string | The name of the source collection or view from which to create the view. The name is not the full namespace of the collection or view; i.e. does not include the database name and implies the same database as the view to create. You must create views in the same database as the source collection. See also
| ||||||||
pipeline |
array | An array that consists of the aggregation pipeline stage(s). The view definition The view definition is public; i.e. See also
| ||||||||
collation |
Specifies the default collation for the collection or the view. Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks. The collation option When specifying collation, the If you specify a collation at the collection level:
If no collation is specified for the collection or for the operations, MongoDB uses the simple binary comparison used in prior versions for string comparisons. For a view, if no collation is specified, the view’s default collation is the “simple” binary comparison collator. For a view on a collection, the view does not inherit the collection’s collation settings. For a view on another view, the to be created view must specify the same collation settings. After you create the collection or the view, you cannot update its default collation. For an example that specifies the default collation during the creation of a collection, see Specify Collation.
| |||||||||
writeConcern |
document |
When issued on a sharded cluster, | ||||||||
comment |
any |
A comment can be any valid BSON type (string, integer, object, array, etc).
|
The db.createCollection()
method and the db.createView()
method wrap the create
command.
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
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. This lock blocks creation or modification of views in the database until the command completes.
Prior to MongoDB 4.2, create
obtained an exclusive lock on the parent database, blocking all operations on the database and
all its collections until the operation completed.
Changed in version 4.4.
Starting in MongoDB 4.4 with feature compatibility version (fcv) "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 create
in a transaction, the transaction must use read concern "local"
. If you specify a read concern level other than "local"
, the transaction fails.
If the deployment enforces authentication/authorization, create
requires the following privileges:
Required Privileges | |
---|---|
Create a non-capped collection |
|
Create a capped collection |
|
Create a view |
However, if the user has the |
A user with the readWrite
built in role on the database has the required privileges to run the listed operations. 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:
Note
The view created by this command does not refer to materialized views. For discussion of on-demand materialized views, see $merge
instead.
Changed in version 4.2.
The view definition pipeline
cannot include the $out
or the $merge
stage. 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.
To create a view using the create
command, use the following syntax:
or if specifying a collation:
For example, given a collection survey
with the following documents:
The following operation creates a managementRatings
view with the _id
, feedback.management
, and department
fields:
Important
The view definition is public; i.e. db.getCollectionInfos()
and explain
operations on the view will include the pipeline that defines the view. As such, avoid referring directly to sensitive fields and values in view definitions.
See also参阅
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
:
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()
. 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. See the WiredTiger documentation of collection level options for specific wiredTiger
options.