- Reference >
- JavaScript Methods >
- db.createCollection()
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
sizeparameter. - size (bytes) – If
cappedistrue,sizespecifies a maximum size in bytes for the capped collection. Whencappedis false, you may usesizeto preallocate space. - max (int) – Optional. Specifies a maximum “cap,” in number of
documents for capped collections. You must also
specify
sizewhen specifyingmax.
Options: - autoIndexId – If
cappedistrueyou can specifyfalseto disable the automatic index created on the_idfield. Before 2.2, the default value forautoIndexIdwasfalse. 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:
This command creates a collection named
logwith 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:This command provides a wrapper around the database command
create. See Capped Collections for more information about capped collections.