NoSQL databases and MongoDB

Introduction

NoSQL databases, or non-relational databases, have gained popularity and widespread adoption in the past decade. One of the most widely used NoSQL databases, MongoDB, ranked fifth on DB-Engines’ list and was the highest ranked of the four non-relational databases included in the top 10.

Learning Objectives

  • What is MongoDB?
  • MongoDB vs SQL structure – Anatomy
  • Why to use MongoDB ? and When?
  • Installation

Study

So what actually is MongoDB?

MongoDB is an open source NoSQL database that stores data in documents similar to JSON objects. When I say similar to JSON, it does not mean exactly JSON, but it means JSON-like storage format known as binary JSON or BSON (a superset of JSON with some more data types, most importantly binary byte array).

Here’s a simple JSON document describing a User.

{
  "_id": 1,
  "name": {
    "first": "Ada",
    "last": "Lovelace"
  },
  "title": "The First Programmer",
  "interests": ["mathematics", "programming"]
}

Anatomy of MongoDB

  • Within a MongoDB instance, you can have zero or more databases.
  • A database can have zero or more collections.
  • Collections are made up of documents.
  • Each document contains pairs of fields and values.
  • The values can typically be a variety of types such as string, number, boolean, date, array or objects.
  • The fields can vary from document to document and developers can change the structure of the database easily as needs change over time.

Getting started with MongoDB | Practical Data Analysis

For illustration purpose, let’s take an example of student table from SQL structure, and transform into the MongoDB structure

Mapping Relational Databases to MongoDB
MongoDB vs. SQL Server 2008: A .NET Developer's Perspective – Aaronontheweb

Terminologies

Collections

‘Collections’ in Mongo are equivalent to tables in relational databases. They can hold multiple JSON documents.

Documents

‘Documents’ are equivalent to records or rows of data in SQL. While a SQL row can reference data in other tables, Mongo documents usually combine that in a document.

Fields

‘Fields’ or attributes are similar to columns in a SQL table.

Schema

While Mongo is schema-less, SQL defines a schema via the table definition. A Mongoose ‘schema’ is a document data structure (or shape of the document) that is enforced via the application layer.

Models

‘Models’ are higher-order constructors that take a schema and create an instance of a document equivalent to records in a relational database.

Why do developers love MongoDB?

MongoDB has become one of the most popular databases in the world because developers really like using it to create applications. Here are a few reasons why developers learn and use MongoDB.

  • It has a schemaless document data model and it allows programmers to change the structure of the database easily.

    • (Though MongoDB is schemaless, you should still add a schema on the server to track the fields because it is very important to keep data organized and documented.)
  • It helps you to handle large amounts of unstructured data.

  • Scalability and Availability — MongoDB’s horizontal scale-out architecture can support huge volumes of data and traffic.

  • Ad-hoc Queries — MongoDB supports, field, range query and regular-expression searches.

  • It can be used with all major programming languages like C, C++, C#, Java, Node.js, Perl, PHP, Python, Ruby, Scala, .NET, Haskell, Go and Erlang.

  • It is open-source and has a huge community of developers and consultants, so it is easy to get help.

When should you use it?

  • It is better to use MongoDB,When your application needs to handle data that have high volume and velocity
  • When you are integrating large amounts of diverse data from different data sources, the document-based model of MongoDB can create a unified single view
  • When you have an application with different kinds of data fields and you may need to add new data fields
  • When you want faster read and write queries
  • When you are delivering data in high-performance applications.

MongoDB was built for people who are building applications that need to evolve quickly and scale elegantly. If you are doing that, you should consider MongoDB.