Tuesday, 1 July 2014

Getting Started with MongoDB

Introduction

From Wiki

MongoDB (from "humongous") is a cross-platform document-oriented database. Classified as a NoSQL database, MongoDB eschews the traditional table-based relational database structure in favor of JSON-like documents with dynamic schemas (MongoDB calls the format BSON), making the integration of data in certain types of applications easier and faster. Released under a combination of the GNU Affero General Public License and the Apache License, MongoDB is free and open-source software.


Getting Started

  1. Download MongoDB from their website and install it. You have installer file for windows or zip file. Either way you should get MongoDB 2.6 Standard  folder installed. My folder location is C:\Program Files\MongoDB 2.6 Standard. You can find a bin file inside this folder. Add it to the %PATH% environment variable (windows) or export it by adding in .bashrc file(Linux).
  2. Next open command prompt and run MongoDB server. To do so execute command

    mongod --dbpath C:\Users\athakur\Softwares\MongoDB

    Replace the path after --dbpath with your path. It basically says where your db files will be stored. If you do not provide db path your server will not start and will show that dbpath was missing.


    After starting the server with --dbpath


    You may be asked to allow network access to mongo db. Provide the required access.

  3. Next start the client by typing

    mongoThat should open the Mongo shell.


    By default, mongo looks for a database server listening on port 27017 on the localhost interface. To connect to a server on a different port or interface, use the --port and --host options.
  4. Lets create a new database. Type the following command

    use mydb

    and then type

    db

    This should show mydb database. Now try doing

    show dbs

    Nothing? Surprised? Don't be. It is the way Mongo DB behaves. MongoDB will not permanently create a database until you insert data into that database.

    Note :
    Unlike MySQL database you do not create tables and then insert data. Infact there is no relational model here. You simply switch db using use dbName syntax and add data. When you actually add the data the database is created.

  5. Lets go ahead and insert some data in our database. First create documents doc1 and doc2 using

    doc1 = {name : "Aniket"}
    doc2 = {org : "openSourceForGeeks"}


    Then add these documents to the collection (lets call it testDataCollection) database using

    db.testDataCollection.insert( doc1 )
    db.testDataCollection.insert( doc2 )


        When you insert the first document, the mongod will create both the mydb database and the testDataCollection collection.

    You can view your collections by

    show collections

    Confirm that your testDataCollection collection is created.

    The mongo shell will return the list of the collections in the current (i.e. mydb) database. At this point, the only collection is testDataCollection . All mongod databases also have a system.indexes collection.

    Also confirm the database is created ( by show dbs).

  6. Data Stored in MongoDB is essentially JSON. If you wish to see the data in the collection you can use command

    db.testDataCollection.find()

  7. That;s all for basics of mongo db. We say how to create database, collections and insert data in it. We also saw how can we retrieve the data.


Important commands

  1. db - shows current db in use
  2. help - show help
  3. cls - clear screen
  4. exit - exit Mongo shell
  5. show dbs - Show list of databases
  6. use mydb - use some other database
  7. show collections - Show all collection for the db. 

Important Links

No comments:

Post a Comment

t> UA-39527780-1 back to top