- 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
size
parameter. - size (bytes) – If
capped
istrue
,size
specifies a maximum size in bytes for the capped collection. Whencapped
is false, you may usesize
to preallocate space. - max (int) – Optional. Specifies a maximum “cap,” in number of
documents for capped collections. You must also
specify
size
when specifyingmax
.
Options: - autoIndexId – If
capped
istrue
you can specifyfalse
to disable the automatic index created on the_id
field. Before 2.2, the default value forautoIndexId
wasfalse
. 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
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
:This command provides a wrapper around the database command
create
. See Capped Collections for more information about capped collections.