MongoDB: db.createCollection() method


The db.createCollection() method is used to create a new collection. Commonly used to create a capped collection.

A capped collection is a fixed-sized collection that automatically overwrites its oldest entries when it reaches its maximum size.  Once a collection fills its allocated space, it makes room for new documents by overwriting the oldest documents in the collection.


db.createCollection(name, options)

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

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


Name Description Required /
name The name of the collection to create. Required string
options Configuration options for creating a capped collection or for preallocating space in a new collection. Optional document

The options document contains the following fields:

Field Description Required /
capped To create a capped collection, specify true. If you specify true, you must also set a maximum size in the size field. Optional boolean
autoIndexId Specify false to disable the automatic creation of an index on the _id field.
Note: For replica sets, all collections must have autoIndexId set to true.
Optional boolean
size pecify 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. Optional number
max 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. Optional number
noPadding Available for the MMAPv1 storage engine only.
Allows users to specify configuration to the storage engine on a per-collection basis when creating a collection. The value of the storageEngine option 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.
Optional boolean

Example: MongoDB: db.createCollection() method

The following example will create a new collection named collection_new to the current database.

 db.createCollection("collection_new", { capped : true, size : 5242880, max : 5000 } );


> db.createCollection("collection_new", { capped : true, size : 5242880, max : 5000 } );
{ "ok" : 1 }

To show the newly created collection the the following command. 

> show collections;

Retrieve the restaurants data from here

Previous: db.copyDatabase() methos
Next: db.currentOp() method

Share this Tutorial / Exercise on : Facebook and Twitter