Create Collactions



db.createCollection (nama, pilihan)
Menciptakan koleksi baru secara eksplisit.

Karena MongoDB menciptakan koleksi secara implisit ketika koleksi pertama dirujuk dalam perintah, metode ini digunakan terutama untuk menciptakan koleksi capped baru. Ini juga digunakan untuk pra-mengalokasikan ruang untuk koleksi biasa.

The db.createCollection () metode memiliki bentuk prototipe berikut:

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

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. To create a capped collection, specify true. If you specifytrue, 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.

Do not set autoIndexId to true for replicated collections.

size number 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 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 the size limit before it reaches the maximum number of documents, MongoDB removes old documents. If you prefer to use the max limit, ensure that the size limit, which is required for a capped collection, is sufficient to contain the maximum number of documents.
usePowerOf2Sizes boolean

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 is true. The usePowerOf2Sizes option does not affect the allocation strategy.

noPadding boolean

Optional. Available for the MMAPv1 storage engine only.

New in version 3.0: noPadding flag disables the power of 2 sizes allocation for the collection. With noPadding flag 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.


Do not set noPadding if the workload includes removes or any updates that may cause documents to grow. For more information, see No Padding Allocation Strategy.

storageEngine document

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 thestorageEngine option should take the following form:

{ <storage-engine-name>: <options> }

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.

Facebook Comments

Leave a Reply