OPTIONS
翻译或纠错本页面

db.createCollection()

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. This is also used to pre-allocate space for an ordinary collection.

The db.createCollection() method has the following prototype form:

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

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. Enables a capped collection. To create a capped collection, specify true. If you specify true, 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. Before 2.2, the default value for autoIndexId was false. See _id Fields and Indexes on Capped Collections for more information.

Do not set autoIndexId to true for replicated collections.

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

Optional.

2.6 新版功能: usePowerOf2Sizes became an option to db.createCollection() when usePowerOf2Sizes became the default allocation strategy for all new collections by default.

Set to false to disable the usePowerOf2Sizes allocation strategy for this collection. Defaults to true unless the newCollectionsUsePowerOf2Sizes parameter is set to false.

Example

The following example creates 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:

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 限制集 for more information about capped collections.