Navigation

create

Definition

create

Explicitly creates a collection. create has the following form:

Changed in version 3.2.

{
  create: <collection_name>,
  capped: <true|false>,
  autoIndexId: <true|false>,
  size: <max_size>,
  max: <max_documents>,
  flags: <0|1|2|3>,
  storageEngine: <document>,
  validator: <document>,
  validationLevel: <string>,
  validationAction: <string>,
  indexOptionDefaults: <document>
}

create has the following fields:

Field Type Description
create string The name of the new collection.
capped boolean Optional. To create a capped collection, specify true. If you specify true, you must also set a maximum size in the size field.
autoIndexId boolean

Optional. Specify false to disable the automatic creation of an index on the _id field.

Important

For replica sets, do not set autoIndexId to false.

Deprecated since version 3.2: The autoIndexId option will be removed in version 3.4.

size integer Optional. Specify a maximum size in bytes for a capped collection. Once a capped collection reaches its maximum size, MongoDB removes the older documents to make space for the new documents. The size field is required for capped collections and ignored for other collections.
max integer Optional. The maximum number of documents allowed in the capped collection. The 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.
flags integer

Optional. Available for the MMAPv1 storage engine only to set the usePowerOf2Sizes and the noPadding flags. To set, specify one of the following values:

Note

MongoDB 3.0 ignores the usePowerOf2Sizes flag. See collMod and db.createCollection() for more information.

Defaults to 1.

New in version 2.6.

Changed in version 3.0.0: Add support for setting the new noPadding flag.

Warning

Do not set noPadding if the workload includes removes or any updates that may cause documents to grow. For more information, see No Padding Allocation Strategy.

storageEngine document

Optional. Available for the WiredTiger storage engine only.

New in version 3.0.

Allows users to specify configuration to the storage engine on a per-collection basis when creating a collection. The value of the storageEngine option should take the following form:

{ <storage-engine-name>: <options> }

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

Optional. Allows users to specify validation rules or expressions for the collection. For more information, see Document Validation.

New in version 3.2.

The validator option takes a document that specifies the validation rules or expressions. You can specify the expressions using the same operators as the query operators with the exception of $geoNear, $near, $nearSphere, $text, and $where.

Note

  • Validation occurs during updates and inserts. Existing documents do not undergo validation checks until modification.
  • You cannot specify a validator for collections in the admin, local, and config databases.
  • You cannot specify a validator for system.* collections.
validationLevel string

Optional. Determines how strictly MongoDB applies the validation rules to existing documents during an update.

New in version 3.2.

validationLevel Description
"off" No validation for inserts or updates.
"strict" Default Apply validation rules to all inserts and all updates.
"moderate" Apply validation rules to inserts and to updates on existing valid documents. Do not apply rules to updates on existing invalid documents.
validationAction string

Optional. Determines whether to error on invalid documents or just warn about the violations but allow invalid documents to be inserted.

New in version 3.2.

Important

Validation of documents only applies to those documents as determined by the validationLevel.

validationAction Description
"error" Default Documents must pass validation before the write occurs. Otherwise, the write operation fails.
"warn" Documents do not have to pass validation. If the document fails validation, the write operation logs the validation failure.
indexOptionDefaults document

Optional. Allows users to specify a default configuration for indexes when creating a collection.

The indexOptionDefaults option accepts a storageEngine document, which should take the following form:

{ <storage-engine-name>: <options> }

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.

New in version 3.2.

The db.createCollection() method wraps the create command.

Considerations

The create command obtains a write lock on the affected database and will block other operations until it has completed. The write lock for this operation is typically short lived. However, allocations for large capped collections may take longer.

Examples

Create a Capped Collection

To create a capped collection limited to 64 kilobytes, issue the command in the following form:

db.runCommand( { create: "collection", capped: true, size: 64 * 1024 } )

Specify Storage Engine Options

New in version 3.0.

You can specify collection-specific storage engine configuration options when you create a collection with 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. See the WiredTiger documentation of collection level options for specific wiredTiger options.

←   drop clone  →