Introduction to MongoDB

We are very familiar with relational databases. I would like share details of MongoDB databases.

  1. What is MongoDB?

    MongoDB is a high performance, scalable, open source, document-oriented database. It is in the NoSQL database category. Document oriented doesn't mean that it stores information in documents like PDF, Word documents and HTML. Instead MongoDB stores information in BSON documents. BSON is a binary-encoded serialization of JSON-like documents. BSON is designed to be lightweight, traversable and efficient.

    for example { "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }

    MongoDB was originally developed by the company 10gen.Now but it is now MongoDB Inc.

  2. MongoDB Vs RDBMs

    We can choose MongoDB over relational databases for the following factors:

    1. Horizontal Scalability: Relational databases have limited horizontal scalability, whereas MongoDB supports horizontal scalability where we can add as many machines as needed. Horizontal scalability is very important to deal with Big Data.

    2. High Performance: This is because MongoDB stores as much data in RAM as possible and so it becomes as fast as using something like memcached for the cached data. Some analysis proves that it has better performance over relational databases for read and write operations.

    3. Flexibility: MongoDB uses a flexible schema document structure that provides flexibility for future structural changes. Whereas in relational databases, we use a schema first approach.

      Terminology used in MongoDB



  3. Platform and language supported

    It is supported by the following platforms: UNIX, Mac and Windows. It supports both 32 bit and 64 bit architectures. It is recommended to use 64 bit builds in production environments. MongoDB supports many popular languages like C, C++, C#, Java, JavaScript, PHP, Python, Ruby, Scala and so on. They have drivers for communication with MongoDB databases using these languages.

  4. Features of MongoDB

    1. Querying Support: Usually there is the misconception that NoSQL databases lack querying support. MongoDB does provide query language support. The following example retrieves data from the people collection documents where the name field has the value john: - db.people.find( { name: "john" } )

    2. Indexing: MongoDB does support indexing that provides high performance in read operations. Indexes are particularly useful where the total size of the documents exceeds the amount of available RAM. These indexes use a B-tree data structure. Here is a sample MongoDB document:

      {"_id": ObjectId(4c2209fef3924d31102bd84b)
         "name": "John Doe"
         "address": {
            "street": "Main"
            "zipcode": 53511
            "state": "WI"
         }
      }

      You can create an index on the address.zipcode field, using the following specification:

      db.people.ensureIndex( { "address.zipcode": 1 } )

      The _id index is a unique index on the _id field, and MongoDB creates this index by default on all collections. You cannot delete the index on _id. The _id field is the primary key for the collection and every document must have a unique _id field

    3. Replication: Replication causes data redundancy across multiple servers but is helpful for high availability. Here the primary server is responsible for all write operations. The primary replicates all the data to secondary servers. In the case of a primary server failure, one of secondary servers are elected as the primary server.



    4. Sharding: Sharding is a method for storing data across multiple machines. Sharding divides the huge data and distributes the data over multiple servers (called shards). Each shard is an independent database and collectively all shards make up a single logical database.



  5. Recommend/Use MongoDB

    When you need high performance, horizontal scalability or flexible schema, MongoDB is a good choice. Here are some applications where it can be used:

         
    1. Archiving and Event Logging
    2. Document/Content Management
    3. Gaming
    4. Agile Development
    5. Real time stats and analysis

  6. Do not recommend/use MongoDB

    MongoDB doesn't support transactions and SQL Joins; that's why it is not recommended to be used in applications like:

    1. Complex Transaction applications, for example Banking or Accounting
    2. Applications that require complex joins