Create Collection in MongoDB

Collections are like that of tables of RDBMS and are capable enough to store documents of diverse or dissimilar types. Creation and removal of collections in MongoDB can be done in specific ways. In this chapter, you will learn about the creation of collections in a database created using MongoDB.

Creating a Collection in MongoDB

Creation of collection can be done using db.createCollection(name, options).  But, typically you will not require building or constructing a collection of your own. MongoDB does this creation task automatically as you start to insert some documents for making a database. Here is a syntax that will tell you how to create your collection in MongoDB:

Syntax:
db.createCollection(collection_name, options)

Here, db.createCollection() is the method used; "name" is a data type - string which is specifying the name of the collection to be formed. "options" is an added document type which helps in specifying the size of memory along with indexing the collection in the database. This parameter is an optional one.

The following example shows the syntax of the createCollection() method with its options:

Example:
db.createCollection(<collection_name>, { capped: <boolean>,
                              autoIndexId: <boolean>,
                              size: <number>,
                              max: <number>,
                              storageEngine: <document>,
                              validator: <document>,
                              validationLevel: <string>,
                              validationAction: <string>,
                              indexOptionDefaults: <document>,
                              viewOn: <string>,
                              pipeline: <pipeline>,
                              collation: <document>,
                              writeConcern: <document>} )

Here is the detailing of some important fields that can be used as options in the create createCollection() method:

Field for optionTypeDescription
cappedBooleanCapped collection is a predetermined size of the collection which gets automatically overwritten to its oldest entries as it attains a maximum size. When it is set to True, the capped collection feature gets enabled.
autoIndexIdBooleanWhen this option is set to true, it automatically generates an index on the ID field. By default, its value is kept as false.
sizeNumberThis option is used to specify the ceiling size in bytes for capped collection. When the capped value is set as true, you are required to specify this field also.
maxNumberThis option helps to specify the highest number of documents allocated for a capped collection.

Let us take an example to see how to implement the command in MongoDB:

Example:
db.createCollection("MyCollection")
Output:

MongoDB can create collections automatically as documents are inserted automatically. Let us take a situation where you want to insert a document using insert() in a collection named "movie"; you can type the command something like this:

Example:
db.movie.insert({"name":"Avengers: Endgame"})

The above operation will automatically create a collection if the collection with this name does not currently exist. Also, if you want to check an inserted document, you can use the find() method. Its syntax is:

Syntax:
db.collection_name.find()
Output:


Scroll Back to Top