Tuesday, 5 February 2013

MongoDB Tutorial


       MongoDB is an open-source, document-oriented database designed for ease of development, scaling and NoSql. It is written in C++.

       The Manual introduces MongoDB and continues to describe the query language, operational considerations and procedures, administration, and application development patterns, among other topics relevant to users and administrators of MongoDB. The Manual also has a thorough reference section of the MongoDB interface and tools.

Terminology and concepts:

    SQL Db           MongoDb
      Tables                 Collection
      Row                    Document
      Column                 Field 
      Table Joins            Embedded documents and linking
      primary Key            Primary Key
      Specify unique Column  _id field is automatically set

You can also explicitly create a collection:
        db.createCollection(‘users’);

Installing MongoDB

Download MongoDB from Click here

Instructions:

1. The MongoDB folder name will be either: C:\mongodb-win32-i386-[version] or C:\mongodb-win32-x86_64-[version]
Now start the command prompt using Administrator and use the following command "cd \ move C:\mongodb-win32-* C:\mongodb"

2. Extract the archive (wherever you want) and navigate to the bin subfolder. Don’t execute anything just yet, but know that mongod is the server process and mongo is the client shell - these are the two executables we’ll be spending most of our time with.

3. Create a new text file in the bin subfolder named mongodb.config

4. Add a path to a single file mongodb.config: dbpath=PATH_TO_WHERE_YOU_WANT_TO_STORE_YOUR_DATABASE_FILES.
5. Launch mongod with the --config /path/to/your/mongodb.config parameter.

Note: Hopefully you now have MongoDB up and running. If you get an error, read the output carefully - the server is quite good at explaining what’s wrong.

Six things to Remember!

1. MongoDB has the same concept of a database with which you are likely already familiar (or a schema for you Oracle folks). Within a MongoDB instance you can have zero or more databases, each acting as high-level containers for everything else.

2. A database can have zero or more collections. A collection shares enough in common with a traditional table that you can safely think of the two as the same thing.
3. Collections are made up of zero or more documents. Again, a document can safely be thought of as a row.

4. A document is made up of one or more fields, which you can probably guess are a lot like columns.

5. Indexes in MongoDB function much like their RDBMS counterparts.

6. Cursors are different than the other five concepts but they are important enough, and often overlooked, that I think they are worthy of their own discussion. The important thing to understand about cursors is that when you ask MongoDB for data, it returns a cursor, which we can do things to, such as counting or skipping ahead, without actually pulling down data.