Introduction to MongoDB

Sooriya Dasanayake
JRC Tech Drive
Published in
4 min readOct 24, 2019

MongoDB is an open-source document-based NoSQL database management system. Before we move on to MongoDB let’s get to know something about NoSQL.

What is NoSQL?

NoSQL is a more advanced database than MySQL, SQL relational databases. You need to map fields, data types before storing data in relational databases like MySQL and SQL, but in NoSQL databases like MongoDB you don’t need to map. NoSQL databases are more scalable and provide higher performance, over a large amount of data. Lighter than relational databases. It is possible to handle rapidly changing structured, semi-structured, and unstructured data which is relational databases cannot perform.

Types of NoSQL database:

Key value databases:

Use a simple data model that pairs unique key and its associated value in storing data elements.

Common uses include: storing clickstream data and application logs.

Ex: Redis, Memcached, Coherence

Graph databases:

A graph database, also called a graph-oriented database. essentially a collection of nodes and edges.

Common uses include: recommendation engines and geospatial applications.

Ex: Titan, IBM Graph, Neo4j

Document databases:

Store data elements in document-like structure that encode information in formats such as JSON.

Common uses include: content management and monitoring Web and mobile applications.

Ex: MongoDB, CouchDB, MarkLogic

Wide-column stores:

Also called table-style databases-store data across table that can have very large numbers of columns.

Common uses include: Internet search, other large-scale Web applications.

Ex: Google BigTable, Cassandra, HBase

History of MongoDB

The development of MongoDB was initially started in early 2007 when the company was developing a Microsoft Azure-like platform as a service. This was a New York-based company named 10gen which has now changed its name to MongoDB Inc. The initial development was focused on building a PaaS (Platform as a Service), but later in 2009, MongoDB came to the market as an open-source database server and was maintained by this organization itself.

In March 2010, the company launched its first ready product which was version 1.4. The latest, as well as the stable version of MongoDB, is version 4.2.0 that was released on 13 August 2019.

What is MongoDB?

MongoDB is written in C++ and an open-source document database and leading NoSQL database.

The advantages of MongoDB are that it is easier to work with large amounts of data, because the data is stored as documents and can work at a much faster than relational database.

MongoDB is used by some of the largest companies in the world Ex: Nokia, Facebook, Google, MTV Networks, Cisco, Forbes.

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

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

Database

Databases hold collections of documents. A single MongoDB server typically has multiple databases.

Collection

Collection is a group of MongoDB documents. It is similar to a RDBMS table. Documents within a collection can have different fields.

Document

A document MongoDB is a set of key-value pairs. A document has a dynamic scheme, which means that documents in the same collection do not need to have the same set of fields or structure. This means one document of a collection can have 4 fields and another document can have 3 fields.

The primary purpose of building MongoDB is:

· Scalability

· Performance

· High Availability

Key points of MongoDB

· Develop Faster

· Deploy Easier

· Scale Bigger

Mapping relational database to MongoDB

Table vs Collection

The _id field is the unique identifier for a document. MongoDB allows for each document to be retrieved using this field. You can supply this id or if not MongoDB will generate it.

An ObjectId is a 12-byte BSON type.

· First 4 bytes representing the current timestamp

· Next 3 bytes are the machine identifier

· Next 2 bytes consists of process id

· Last 3 bytes are a random value

Advantages of MongoDB over RDBMS

· MongoDB is schema-less

· There are no complex joins in MongoDB.

· It is very easy to scale.

· Structure of a single object is clear.

Where to Use MongoDB?

· Big Data

· Content Management and Delivery

· Mobile and Social Infrastructure

· User Data Management

· Data Hub

When not to use MongoDB?

MongoDB is a NoSQL database, and it is not compatible with ACID (atomicity, consistency, isolation, durability). MongoDB cannot be used in applications where ACID compliance is required (ex: applications requiring database-level transactions). Ex: you should not use MongoDB when developing a core-banking system for a bank. And also, MongoDB does not have the capability for stored procedures.

To summarize, we can say that MongoDB is a fast and reliable database. Also, MongoDB is one of the recommended databases when developing scalable web applications that require storing unstructured data. If you need high performance, high availability applications, then you should consider using MongoDB as your application’s backend database.

--

--