createCollection() - Creates a collection

Creates a new collection with the specified characteristics.

Syntax diagram

Read syntax diagramSkip visual syntax diagramdb.collection. createCollection(name,tableSpec)

Command arguments

name
Specifies a collection name. It must be alphanumeric with no special characters except $.
tableSpec
This optional parameter specifies the selection criteria for indexes. The options are as follows:
_id
Optional. Specifies ID definition. If you explicitly specify a type for _id, it is created as that specific type and does not allow other types. If you do not specify the _id type, it is flexible. It must be one of the following data types:
  • $int
  • $long
  • $number
  • $date
  • $timestamp
  • $string: length
  • $binary: length
capped (YES | NO)
Specifies whether the collection is capped. You must also specify the maximum size of the capped collection with the size parameter.
size
Specifies the size of the capped collection in bytes. Size is a required parameter if you are creating a capped collection
max
This optional parameter specifies the maximum number of documents in the collection.
compress (YES | NO | true | false)
Specifies whether to compress the collection.
tablespace
Specifies the name of the table space.
inline
Specifies the buffer pool space usage. It is of type integer.
systemTime (sysStartField | sysEndField)
Specifies the system time for sysStartField and sysEndField variables.
businessTime (busStartField | busEndField )
Specifies the business time for busStartField and busEndField variables.

Example

Example 1: Create a collection that is called "media" with _id of type SQL BIGINT.
db.createCollection(“media”, {_id: “$long”)}) 
Sample output:
Collection: TEST."media" created. Use db.media.