Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Creating database, collections and document in mongodb

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 637
    Comment on it

    In this blog we will learn about some terms and crud operations in mongodb. As we are very much aware about the terms related with RDBMS such as creating database, creating tables and insert records in the table. Here we will learn about the basic terms in mongodb such as “collections” is similar to tables in mongdb, documents are similar to records in mongodb. Here first we will see how to create a database, collection and documents in mongodb.

    First let us start the mongo shell in our terminal:

    username@machinename:~$ mongo
    MongoDB shell version: 3.0.12
    connecting to: test

    Now if we want to check all the database on the server, we use the following command on mongo shell:

    > show dbs

    The above command will give us the list of database

    Now we can select the name of the db in which we want to work.

    >use <db>
    Output: switched to db <databasename>

    If we want to create a new database just use the above command with the database name that we want.

    >use demodb
    Output: switched to db demodb


    Now let us learn how to create collections in the database. In MongoDB collections are created implicitly when we first use the collection name in a command. It pre-allocates space for a collection. For creating a capped collection we need to create a capped collection explicitly using the mong shell helper db.createCollection().Below is the prototype for db.createCollection()

    db.createCollection(<name>, { capped: <boolean>,
                                  autoIndexId: <boolean>,
                                  size: <number>,
                                  max: <number>,
                                  } )
    Below are the meanings for the option in the prototype 
    Name : name for the collection
    Capped: it tells about the collection. That the
    collection is of fixed size which will automatically overwrite its
    oldest entries on reaching its max size.If option capped is choosen
    as true, it is necessary to specify size parameter.
    AutoIndexid: if this option is set to true than
    is creates index automatically to _id field will be created
    Size : size of the document
    Max : It shows that maximum number of documents
    (i.e. records) that we can store in the collection

    Let us create a collection with different specifications as below:
    > db.createCollection("error_log",
    { capped : true, autoIndexId: true, size : 5242880, max : 5000 } )
    { "ok" : 1 }
    Similarly we create another collections
    > db.createCollection("user_profile",
    { capped : true, autoIndexId: true, size : 5242880, max : 5000 } )
    { "ok" : 1 }


    The above command will create a collection named error_logs within the DB demodb having max size of 5 megabytes and it will store max 4000 documents.

    Let us insert one document in the collection user_profile and check find the inserted result as below:

    WriteResult({ "nInserted" : 1 })


    Now let us find the document in the collection user_profile

    { "_id" : ObjectId("57bee56ace1de15c9c8745c6"), "firstname" : "Tom", "lastname" : "shan" }


 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: