Navigation
This version of the documentation is archived and no longer supported.

db.createCollection()

On this page

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.

db.createCollection() is a wrapper around the database command create.

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

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

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

Parameter Type Description
name string The name of the collection to create.
options document 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 Type Description
capped Boolean 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 Boolean

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.

Important

For replica sets, all collections must have autoIndexId set to true.

size number 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 number 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 boolean

Optional. Set to false to disable the usePowerOf2Sizes allocation strategy for this collection. Defaults to true unless the newCollectionsUsePowerOf2Sizes parameter is set to false.

New in version 2.6: usePowerOf2Sizes became an option to db.createCollection() when usePowerOf2Sizes became the default allocation strategy for all new collections.

Example

The following example creates a 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.

See Capped Collections for more information about capped collections.