How to create a simple and capped collection in MongoDB

Introduction :

Collections are used to group MongoDB documents. If you are familier with RDBMS, you can think it like a table. Collections are created within a database. Again, documents are created within collection. These documents contains different different fields with different data. In this tutorial, we will show you how to create a collection with different ways to create different types of collection.

How to create a collection :

The syntax used in MongoDB to create a collection is as below :

db.createCollection(name, options)

Here, name is a string value, name of the collection. options is different configuration options for creating a collection. This is an optional parameter. The prototype form of the createCollection() looks like below :

db.createCollection(, { capped: ,
                              autoIndexId: ,
                              size: ,
                              max: ,
                              storageEngine: ,
                              validator: ,
                              validationLevel: ,
                              validationAction: ,
                              indexOptionDefaults: ,
                              viewOn: ,
                              pipeline: ,
                              collation: ,
                              writeConcern: } )

As you can see that the options argument may have a lot of different properties. Following are the mostly used properties while creating a collection :

  1. capped(boolean) : Optional property. It is used to create capped collections. These are fixed size collections. If the collection size exceeds, they overlaps the old documents. If you set true, you should set the size field.
  2. autoIndexId(boolean) : Deprecated since 3.2. Set it false to disable automatic id creation in __id _ field.
  3. size(number) : Optional but if you are setting true for capped, you need to set this value. It indicates the max size of a capped collection.
  4. max(number) :Optional. Maximum doc allowed in a collection. If the size value is exceeds first before reaching the maximum doc level, MongoDB will start to replace old documents.

You can check the official guide to know more about other properties.

Example to create a simple collection :

Let’s create one simple collection. Run the following command to create a collection of name “myNewCollection” :

db.createCollection("myNewCollection")

To check all available collections, use :

show collections

MongoDB create collection

Example to create a capped collection :

You can think capped collections like a circular buffer. It has fixed size. If the size is exceed, it will replace old documents automatically. Let’s create one capped collection :

db.createCollection("dummyCapped", { capped : true, size : 4194304, max : 10000 })

It will create one capped collection with 4 MB maximum size and maximum documents level 10000. If the size exceeds, it will start overlapping even if the max is less than 10000.

MongoDB create Capped collection

Create a collection with pre allocate memory :

To create a uncapped collection with pre-allocate memory, use :

db.createCollection("users", { size: 4194304 } )

It will create a collection users with 4 MB pre allocate size.

Insert document without creating collection :

In MongoDB, we can insert document without creating a collection. If you insert documents to a collection that is not previously created, it will be created automatically. For example , if students collection is not created and you will insert a document to this collection, MongoDB will create it automatically. MongoDB insert without creating collection Conclusion :

You have learnt how to create a collection in MongoDB using different ways. Run the examples explained above and drop a comment below if you have any queries.