OPTIONS

db.createCollection()

Definition

db.createCollection(name, options)

Creates a new collection explicitly.

Because MongoDB creates a collection implicitly when the collection is first referenced in a command, this method is used primarily for creating new capped collections. This is also used to pre-allocate space for an ordinary collection.

The db.createCollection() method has the following prototype form:

db.createCollection(<name>, { capped: <boolean>,
                              autoIndexId: <boolean>,
                              size: <number>,
                              max: <number>,
                              storageEngine: <document> } )

The db.createCollection() method has the following parameters:

Parameter Description
name The name of the collection to create.
options Optional. Configuration options for creating a capped collection or for preallocating space in a new collection.

The options document creates a capped collection or preallocates space in a new ordinary collection. The options document contains the following fields:

Field Description
capped Optional. Enables a capped collection. 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. See _id Fields and Indexes on Capped Collections for more information.

Do not set autoIndexId to true for replicated collections.

size Optional. Specifies a maximum size in bytes for a capped collection. The size field is required for capped collections, and ignored for other collections.
max Optional. The maximum number of documents allowed 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 prefer to use this limit, ensure that the size limit, which is required, is sufficient to contain the documents limit.
usePowerOf2Sizes

Optional. Available for the MMAPv1 storage engine only.

Deprecated since version 3.0: For the MMAPv1 storage engine, all collections use the power of 2 sizes allocation unless the noPadding option is true. The usePowerOf2Sizes option does not affect the allocation strategy.

noPadding

Optional. Available for the MMAPv1 storage engine only.

New in version 3.0: noPadding flag disables the power of 2 sizes allocation for the collection. With noPadding flag set to true, the allocation strategy does not include additional space to accomodate document growth, as such, document growth will result in new allocation. Use for collections with workloads that are insert-only or insert-and-remove-only.

Defaults to false.

storageEngine

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.

Examples

Create Capped Collection

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:

db.createCollection("log", { capped : true, size : 5242880, max : 5000 } )

This command creates a collection named log with a maximum size of 5 megabytes and a maximum of 5000 documents.

The following command simply pre-allocates a 2-gigabyte, uncapped collection named people:

db.createCollection("people", { size: 2147483648 } )

This command provides a wrapper around the database command create. See Capped Collections for more information about capped collections.

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.createCollection( "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.