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

db.createCollection()

db.createCollection(name[, {capped: <boolean>, size: <value>, max <bytes>}])
Parameters:
  • name (string) – Specifies the name of a collection to create.
  • capped (boolean) – Optional. If this document is present, this command creates a capped collection. The capped argument is a document that contains the following three fields:
  • capped – Enables a collection cap. False by default. If enabled, you must specify a size parameter.
  • size (bytes) – If capped is true, size specifies a maximum size in bytes for the capped collection. When capped is false, you may use size to preallocate space.
  • max (int) – Optional. Specifies a maximum “cap,” in number of documents for capped collections. You must also specify size when specifying max.
Options:
  • autoIndexId – If capped is true you can specify false to disable the automatic index created 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.

Explicitly creates a new collection. Because MongoDB creates collections implicitly when referenced, this command is primarily used for creating new capped collections. In some circumstances, you may use this command to pre-allocate space for an ordinary 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, but may also specify a maximum document count. MongoDB will remove 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.