Before reading this article, I highly recommend reading the previous parts of the series:

MongoDB stores data in a document format. A collection is used to store the documents and is just like data tables of the Relational Database Management Systems (RDBMSs). A collection exists within a database and supports dynamic schemas. Documents within a collection can have different fields.

Documents within a collection

As shown in the preceding image, we have a collection whose name is Test. In this collection, we have 3 documents, but the schema of all the three documents is not the same. A collection may store documents that exist outside the same structure. This is possible because MongoDB is a schema-less database. In a relational database, we must define the schema of the table before inserting any data. However, this is not required in MongoDB to define the schema of collections.

Valid collection Name

Before defining a name for a collection, we should consider the following points:

Let's see some examples.

Collection Name Valid
Demo Yes
#Demo No
Demo_Test Yes
12Demo No
Demo12 Yes

createCollection() Method

The MongoDB createCollection() method is used to create a new collection.

Syntax: createCollection(Collection_Name,Option)

Parameters

Name Type Description
Collection_Name String Name of Collection
Option Document Provide Option about Collection size and Auto index

The Collection_Name parameter is essential but the Option parameter is not essential. It is an optional parameter.

Create collection without option parameters

We can create a collection that contains only a Collection_Name parameter.

First, we show the name of all the available collections.

name of all available collection

Now we create an “Employee” collection using the createCollection() method.

Employee

We can see that now the Employee collection has been created and is now present in the preceding list.

Note: In MongoDB, the createCollection() method is not required to create a new collection. MongoDB will create a new collection automatically when we insert data into a collection. Let's see an example.

show collection

Now we insert some data into the “Building” collection that hasn't yet been created.

insert some data

We can see that the “Building” table has been created. So we can create a collection without using the createCollection() method. Unfortunately, this method has some disadvantages, For example, we can't use option parameters in the collection creation.

Create collection with option parameters

When we create a collection with option parameters, then it is known as a Capped Collection. Capped Collections are collections that can store data in the same order it is inserted. Capped Collections are fixed in size, contain a fixed number of documents and use “auto-FIFO age-Out” terminology. In other words, when the allotted space is fully utilized, the new objects (documents) will replace the older ones in the same order it is inserted. Capped Collections work in a way similar to circular buffers. Once a collection fills its allocated space, it makes room for new documents by overwriting the oldest documents in the collection. Capped Collections support high-throughput operations that insert and retrieve documents based on insertion order.

Benefits of Capped Collections:

The following are the restriction for Capped Collections:

The following is the list of options in a Capped Collection:

Field Name Data Type Description
Capped Boolean If the value of a Capped field is true then that means the collection is a capped type. The default value is false
autoIndexID Boolean If the value is true then an index on the _id field is created automatically. The default value is false.
Size Number The maximum size of the collection is specified in bytes. If we set the Capped field to true then we need to specify this field also.
Max Number The maximum number of documents allowed in the collection

Let's see some examples:

Example 1

create a simple capped collection

In this example, we created a simple Capped Collection whose size is 654321 bytes and contains a maximum of 100 documents.

Example 2

create a capped collection

In the preceding image, we created a Capped Collection and have set the value of the max field to 3 and inserted 3 records. Now, we will insert a new record and examine the contents of the collection.

insert a new record

We can see that the record of “pankaj” has been replaced with the record for “Sanjeev”. This is the property of the Capped Collection as we discussed above.

Show collections method: This method is used to show the name of all collections present in the current database.

Syntax: show collections.

Example

First, we see the name of all collections of the current database.

all collection of current database

Now we create a Demo3 collection and again execute the show collections command.

create Demo3

Now the Demo3 collection is present in the preceding list.

drop () Method: the drop () method removes a specific collection and will return true if the selected collection is dropped successfully, otherwise, it will return false.

Syntax: db.Collection_Name.drop()

Example

First, we look at the names of all the collections of the current database.

collection of current database

Now, we drop an Employee and then the entire Employee1 collection using the drop() method.

drop Employee

We can see that now the Employee and the Employee1 collections are not present in the list.

All the preceding are some basic commands for creating and dropping a collection. The next article explains a new topic.

Thanks for reading this article!

Next article >> MongoDB Data Types (Day 5)