MongoDb Tutorial

Day 4 : Create Collection in MongoDB








Mongodb tutorial series

Overview

In this part of the Learn Mongo Series, we will learn how to create a collection in mongodb. We will be creating a collection with the name details .

Let's create a collection

In mongodb database , There are 2 methods to create a collection. We can either create it using the following command db.createCollection(name, options) or we can create it automatically by inserting the data into a document. We will learn both the methods in detail below :

  • Method 1 - db.createCollection(name, options) This command is used to create a collection manually. It takes 2 parameters :
    • name : This is a required parameter and takes string as input.We pass collection name to be created here.
    • options : This is an optional parameter which if passes can have any of the following choices :
      1. capped : It is optional. It's datatype is boolean . It can be either true or false. Capped collections are fixed size collections which automatically overrides its oldest entry after reaching its maximum size. Default value is false . If capped is true , then it is mandatory for us to give value to size param also as they are inter-dependent. Capped collections are not recommended for sensitive data.
      2. size : It is optional. It's datatype is number .It is used to specify the maximum size for a capped collection. It specifies size in bytes. If capped is true , then it is mandatory to give this parameter.
      3. max : It is optional. It's datatype is number . It is used to specify the maximum number of collection allowed in a capped collection.
      4. autoindexed : It's datatype is boolean . If true, used to create an automatic index on _id field.


    										
    > use demo_db
    switched to db demo_db
    > db.createCollection("details")
    { "ok" : 1 }
    >
    
    										
    									

    Let's see a collection with some parameters :

    										
    > use demo_db
    switched to db demo_db
    > db.createCollection("details_new",{capped : true, size : 12400, max : 10000,autoIndexId : true })
    { "ok" : 1 }
    >
    										
    									

    We can see the created collection using the show collections command as shown below :

    										
    > use demo_db
    switched to db demo_db
    > db.createCollection("details_new",{capped : true, size : 12400, max : 10000,autoIndexId : true })
    { "ok" : 1 }
    >show collections
    details
    details_new
    >
    										
    									

  • Method 2 - Creating collection automatically : We can create a collection automatically by just inserting the data in a mongodb document. The syntax is db.<collection_name>.insert({"key":"value"}) as shown below :

    										
    > use demo_db
    switched to db demo_db
    
    >db.students.insert({"name":"alex"})
    { "ok" : 1 }
    >show collections
    details
    details_new
    students
    >
    										
    									

  • Hurray !! Mission Accomplished. We have successfully created a collection in mongodb.

Summary

In this part of learn mongo series , we learned about how we can create a collection in mongodb. We learned the following commands of mongodb :

  1. use : This command is use to either create a new database or switch to an already existing database.
  2. db.createCollection(name,options) : This command is used to create a collection manually.
  3. db.<collection_name>.insert({"key":"value"}) : This command is used to create the collection automatically by inserting data into the document.
  4. show collections : This command is used to list all the mongodb collections of the current mongodb database on the console.