- Reference >
mongoShell Methods >- Database Methods >
- db.createCollection()
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 commandcreate.The
db.createCollection()method has the following prototype form:The
db.createCollection()method has the following parameters:Parameter Type Description namestring The name of the collection to create. optionsdocument Optional. Configuration options for creating a capped collection or for preallocating space in a new collection. The
optionsdocument creates a capped collection or preallocates space in a new ordinary collection. Theoptionsdocument contains the following fields:Field Type Description cappedboolean Optional. To create a capped collection, specify true. If you specifytrue, you must also set a maximum size in thesizefield.autoIndexIdboolean Optional. Specify
falseto disable the automatic creation of an index on the_idfield.Important
For replica sets, all collections must have
autoIndexIdset totrue.sizenumber Optional. Specify a maximum size in bytes for a capped collection. Once a capped collection reaches its maximum size, MongoDB removes the older documents to make space for the new documents. The sizefield is required for capped collections and ignored for other collections.maxnumber Optional. The maximum number of documents allowed in the capped collection. The sizelimit takes precedence over this limit. If a capped collection reaches thesizelimit before it reaches the maximum number of documents, MongoDB removes old documents. If you prefer to use themaxlimit, ensure that thesizelimit, which is required for a capped collection, is sufficient to contain the maximum number of documents.usePowerOf2Sizesboolean 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
noPaddingoption istrue. TheusePowerOf2Sizesoption does not affect the allocation strategy.noPaddingboolean Optional. Available for the MMAPv1 storage engine only.
New in version 3.0:
noPaddingflag disables the power of 2 sizes allocation for the collection. WithnoPaddingflag set to true, the allocation strategy does not include additional space to accommodate document growth, as such, document growth will result in new allocation. Use for collections with workloads that are insert-only or in-place updates (such as incrementing counters).Defaults to
false.Warning
Do not set
noPaddingif the workload includes removes or any updates that may cause documents to grow. For more information, see No Padding Allocation Strategy.storageEnginedocument 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
storageEngineoption should take the following form: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 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:
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.
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:
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.