OPTIONS

create

Definition

create

Explicitly creates a collection. create has the following form:

{ create: <collection_name>,
  capped: <true|false>,
  autoIndexId: <true|false>,
  size: <max_size>,
  max: <max_documents>,
  flags: <0|1|2|3>
}

create has the following fields:

Field Description
create The name of the new collection.
capped Optional. To create a capped collection. specify true. If you specify true, you must also set a maximum size in the size field.
autoIndexId Optional. Specify false to disable the automatic creation of an index on the _id field. Before 2.2, the default value for autoIndexId was false.
size Optional. The maximum size for the capped collection. Once a capped collection reaches its maximum size, MongoDB overwrites older old documents with new documents. The size field is required for capped collections.
max Optional. The maximum number of documents to keep in the capped collection. The size limit takes precedence over this limit. If a capped collection reaches its maximum size before it reaches the maximum number of documents, MongoDB removes old documents. If you use this limit, ensure that the size limit is sufficient to contain the documents limit.
flags

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.

For more information on the autoIndexId field in versions before 2.2, see _id Fields and Indexes on Capped Collections.

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.

Example

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 } )
←   drop clone  →