MongoDB

Lesson 1
Last Updated : December, 2017

What is a Mongo Database?

Mongdb is a document-oriented model enables MongoDB to store semi-structured data which doesn’t require a fixed schema. It can achieve this through the use of JSON documents.

MongoDB is used by some of the largest companies in the world, including Facebook, Google, Nokia, MTV Networks, Cisco, Forbes, and many more.

MongoDB is also a cross platform DBMS, currently supporting Windows, Mac, Solaris, and various Linux distributions at the time of writing.

A MongoDB database is different to a relational database in that, MongoDB uses a document-oriented model to store data. In the document-oriented model, data is stored within documents of a collection. In the relational model, data is stored within rows of a table.

The Collections

In MongoDB, a collection is a group of documents. A collection typically contains documents that have a similar topic (like Users, Products, Posts, etc).

Collections are therefore, in many ways, similar to tables from the relational model.

The Documents

In MongoDB, documents are stored as JSON documents. JSON (JavaScript Object Notation) is a standard that facilitates data interchange. JSON documents are similar to XML documents in that data can be presented in a hierarchical way, and can be read by humans and computers alike.

Here’s an example of a JSON document. This is what documents inside a MongoDB database look like.

Copy{
    shopname : "best xyz",
    albums : [
                {
                    product : "bananas",
                    price : $4,
                    quantity : "60"
                }, 
                {
                    product: "apples",
                    price : $6,
                    quantity : "50"
                }
            ]
}

The _id field is the unique identifier for a document. MongoDB allows for each document to be retrieved/referenced using this field. You can supply this or let MongoDB generate it.

By using JSON, query results can be easily parsed, with little or no transformation, directly by JavaScript and most popular programming languages. This is because JSON documents use the name/pair, and array conventions that are familiar to most popular programming languages such as C, C++, C#, Java, JavaScript, Perl, Python, and many others. This reduces the amount of business logic that needs to be built into applications that use MongoDB.

Behind the scenes, MongoDB actually stores the JSON documents in a binary-encoded format called BSON. BSON extends JSON through supporting additional data types and to be efficient for encoding and decoding within different languages.

Schemaless

Each JSON document in a collection can contain its own structure. Therefore, there is no fixed schema that limits the type of data that can be entered into a MongoDB database.

This is in contrast to a relational database where, you must create the schema first (i.e. define the tables, columns, data types, etc), before entering any data. If data doesn’t adhere to the schema, it doesn’t go into the database.

In a MongoDB database, there is no rule to say which fields, or how many fields, each document should have. For example, one document from a collection might contain name, address, and phone number, while another document might contain name and email address.

Limitations of Relational databases

  1. In relational database we need to define structure and schema of data first and then only we can process the data.
  2. Relational database systems provides consistency and integrity of data by enforcing ACID properties (Atomicity, Consistency, Isolation and Durability ). There are some scenarios where this is useful like banking system. However in most of the other cases these properties are significant performance overhead and can make your database response very slow.
  3. Most of the applications store their data in JSON format and RDBMS don’t provide you a better way of performing operations such as create, insert, update, delete etc on this data. On the other hand NoSQL store their data in JSON format, which is compatible with most of the today’s world application.

What are the advantages of NoSQL

There are several advantages of working with NoSQL databases such as MongoDB and Cassandra. The main advantages are high scalability and high availability.

High scalability:

NoSQL database such as MongoDB uses sharding for horizontal scaling. Sharding is partitioning of data and placing it on multiple machines in such a way that the order of the data is preserved. Vertical scaling means adding more resources to the existing machine while horizontal scaling means adding more machines to handle the data. Vertical scaling is not that easy to implement, on the other hand horizontal scaling is easy to implement. Horizontal scaling database examples: MongoDB, Cassandra etc. Because of this feature NoSQL can handle huge amount of data, as the data grows NoSQL scale itself to handle that data in efficient manner.

Availability:

Auto replication feature in MongoDB makes it highly available because in case of any failure data replicates itself to the previous consistent state.