Before reading this article, I highly recommend reading the previous parts of the series:
- MongoDB - Day 1 (Introduction To MongoDB)
- MongoDB - Day 2 (Install MongoDB in Windows)
- MongoDB - Day 3 (Database Basics)
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.
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:
- Collection names must begin with letters or an underscore. Collection names can't start with a number or special character.
- The "$” symbol can't be used in a collection name because “$” is already reserved.
- The name of a collection can't exceed 128 characters.
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.
Now we create an “Employee” collection using the createCollection() method.
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.
Now we insert some data into the “Building” collection that hasn't yet been created.
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:









Sr KarthigaPosted Feb 14, 2016, 7:43 AM
Good one sir
Pankaj Kumar ChoudharyPosted Aug 5, 2015, 8:46 PM
Thanks sreenivasa k .........
sreenivasa kPosted Aug 5, 2015, 6:19 PM
nice one
Pankaj Kumar ChoudharyPosted Aug 5, 2015, 2:11 PM
Thanks Sibeesh Venu Sir..........
Sibeesh VenuPosted Aug 5, 2015, 6:32 AM
Nice Share
Pankaj Kumar ChoudharyPosted Aug 5, 2015, 5:00 AM
Thanks Debasis Saha Sir..........
Debasis SahaPosted Aug 5, 2015, 1:03 AM
Nice one..
Pankaj Kumar ChoudharyPosted Aug 4, 2015, 11:12 PM
THANKS SANTHAKUMARR SIR..........
Santhakumar MunuswamyPosted Aug 4, 2015, 10:04 PM
Good Article. Thanks for sharing :)
Pankaj Kumar ChoudharyPosted Aug 4, 2015, 9:19 PM
Thanks Chervine Bhiwoo Sir.......
Pankaj Kumar ChoudharyPosted Aug 4, 2015, 9:18 PM
Thanks Rakesh Sir........
Pankaj Kumar ChoudharyPosted Aug 4, 2015, 9:18 PM
Thanks Manoj Kulkarni.......
RakeshPosted Aug 4, 2015, 2:00 PM
Good article
Chervine BhiwooPosted Aug 4, 2015, 1:50 PM
Nice!
Manoj KulkarniPosted Aug 4, 2015, 6:42 AM
Nice article. Thank you for sharing.
Pankaj Kumar ChoudharyPosted Aug 4, 2015, 6:28 AM
Thanks Gopi Chand Sir.........
Pankaj Kumar ChoudharyPosted Aug 4, 2015, 6:28 AM
Thanks @Rajeesh Sir
Pankaj Kumar ChoudharyPosted Aug 4, 2015, 6:27 AM
Thanks Mohammed Ibrahim Sir.........
Gopi ChandPosted Aug 4, 2015, 6:19 AM
Good going Pankaj..keep it up :)
Rajeesh MenothPosted Aug 4, 2015, 5:31 AM
Good Pankaj Kumar Choudhary
Mohammed IbrahimPosted Aug 4, 2015, 4:32 AM
nice