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
    • 765
    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:

    1. username@machinename:~$ mongo
    2. Output:
    3. MongoDB shell version: 3.0.12
    4. connecting to: test
    5.  

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

    1. > 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.

    1. >use <db>
    2. 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.

    1. >use demodb
    2. 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()

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

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

    1. db.user_profile.insert({"firstname":"Tom","lastname":"shan"})
    2. Output:
    3. WriteResult({ "nInserted" : 1 })

     

    Now let us find the document in the collection user_profile

    1. db.user_profile.find()
    2. Output:
    3. { "_id" : ObjectId("57bee56ace1de15c9c8745c6"), "firstname" : "Tom", "lastname" : "shan" }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: