What is MongoDB and Is MongoDB a relational database?

In this MongoDB tutorial, we are going to learn about what is MongoDB, How we can work with MongoDB and Is MongoDB a relational database. Also by taking different scenarios we are going to cover the following topics.

  • what is database, collection and document
  • relational vs non-relational database
  • diffrence between RDBMS and MongoDB
  • is MongoDB a relational database

What is MongoDB

MongoDB is an open-source document-oriented database that is designed to store a large scale of data and allow you to work with the data very efficiently.

It is a cross-platform, document-oriented database that provides, high performance, high availability, and easy scalability. It is classified under the NoSQL (Not only SQL) database because the storage and retrieval of data in MongoDB are not in the form of tables. 

The MongoDB database is released in February 2009. It is managed by MongoDB.Inc under SSPL(Server Side Public License). It is an open-source product, developed and supported by a company named 10gen.

It provides official driver support for all the popular languages like C, C++, C#, and .Net, Goes, Java, Node.js, PHP, Python, Ruby, Scala, Swift, etc. So, that you can easily create an application using any of these languages.

There are so many companies that used MongoDB like Facebook, Google, Uber, Adobe, Accenture, Nokia, etc.

Source: Mongodb.com

The main reason for using MongoDB is

  • highly scalability
  • good performance
  • high availability
  • develop faster
  • scaling from single server deployments to large

How MongoDB works?

Now, we will see how things happen behind the scene. We know that It works on the concept of collection and document and MongoDB is a database server and data is stored in these databases. MongoDB environment gives you a server that you can start and then create multiple databases using MongoDB. Its NoSQL database, the data is stored in the collections and documents.

In the diagram, the database, collection, and documents are related to each other as shown below:

How MongoDB works
What is MongoDB

What is Database, Collection and Documents in MongoDB

Let us try to understand here, what is a database, collection, and documents in MongoDB.

Database

A single MongoDB server typically has multiple databases. The database is a physical container for collections. Each database gets its own set of files on the file system.

Collection

A collection is a group of MongoDB documents. It is the equivalent of an RDBMS (Relational database management system) table. A collection exists within a single database. A collection does not enforce a specific kind of document as a schema does. So one document collection can have a document with user data or a document with log data or whatsoever. Typically, all documents in a collection are of similar or related purposes.

Document

A document is a set of key-value pairs. Documents have a dynamic schema. Dynamic schema means within the same collection do not need to have the same set of fields or structure and common fields in a collection’s documents may hold different types of data. You can check the below example this is how the values are stored in key-value pairs.

{
    name: "Tom",
    age: 23,
    sex: "Male",
    sports: ["Cricket", "Basketball"],
    city: "United States of America"
}

Relational vs Non-Relational Databases

What is a Relational Database(RDBMS)

The RDBMS stands for “Relational Database Management System”. A relational database is a collection of data items with pre-defined relationships between them.

In a relational database, each row in the table is a record with a unique ID called the key. The columns of the table hold attributes of the data, and each record usually has a value for each attribute, making it easy to establish the relationships among data points.

It stores data in form of entities as tables. RDBMS used SQL language to query databases.

Examples of popular RDBMS are oracle, SQL Server, MySQL, etc.

A relational database stores data in tables. The relationship between each data point is clear and searching with that relationship is relatively easy. The relationship between tables and field types is called a schema. Let’s look at an example:

Here we can see that three tables, all are providing unique information on a specific employee. A relational database user can then obtain a view of the database requirements to their needs. For example, I might want to view all the employee whose salary is greater than 50,000. Relational databases make answering questions like these relatively easy.

NameAgeGender
James24Male
Robert22Male
Michael26Male
EmpIDSalaryPlace
10134,000United Kingdom
10248,000Australia
10351,000New Zealand
EmpIDNameGenderAgePlace
101JamesMale24 United Kingdom
102RobertMale22 Australia
103MichaelMale26 New Zealand

Relational databases are also known as SQL databases. SQL stands for Structured Query Language and it’s the language relational databases are written in. SQL is used to execute queries, creating new records, deleting records, retrieving data, edit data by updating.

Advantages

  • It can handle lots of complex queries.
  • It works on the property of ACID (Atomity, Consistency, Isolation, Durability) that makes reliable database transaction.

Disadvantages

  • It can not store complex or large images, numbers and multimedia products.
  • It can become very costly with new servers and maintenences.

What is Non-Relational Database

Non-relational databases are different from relational databases because they store the data in a non-tabular form. A non-relational database is based on data structure like documents. This document is highly detailed with a different type of information in different formats.

It provides a facility to store various types of information that why non-relational databases are much more flexible than relational databases.

There are some of the companies that use the NoSQL database are Amazon, Adobe, Capgemini, SAP, Qualcomm, etc.

We can consider this example:

KeyValue
NameJoe
Age42
OccupationEngineer
Height175cm
Weight77kg

It is a basic NoSQL database example. This will help the developer to store schema-less data.

Difference between RDBMS and MongoDB

Sr.
No.
KeyRDBMSMongoDB
1ConceptRDBMS is a relational database management system and It is used to store data in the form of tables.MongoDB is a non-relational database management system and document-based database.
2PrincipleRDBMS follows the ACID principle Atomicity, Consistency, Isolation, and DurabilityMongoDB follows the CAP theorem, Consistency, Availability, and Partition tolerance.
3PerformanceRDBMS is slower in processing large amounts of data.MongoDB is fast in processing large hierarchical data.
4SchemaIn RDBMS before using a database we need to define the schema.In MongoDB schema can be dynamically created and accessed in MongoDB.
5HierarchicalIn RDBMS, Difficult to store hierarchical data.MongoDB, Have inbuilt support to store hierarchical data.
6Query LanguageRDBMS uses SQL to query databases.MongoDB uses BSON to query the database.

Also, check: What is MariaDB

Is MongoDB a Relational Database

No, MongoDB is not a relational database, It is a non-relational and document-oriented database. It comes in the classification of a NoSQL database.

MongoDB uses BSON (document storage format) that is a binary style of JSON documents. Documents in MongoDB is much like JSON, as shown below:

{
    _id: <ObjectsId1>,
    username: "xyz",
    contact:  {
                phone: "123-456-7890",
                email: "xyz@example.com"
              },
    access: {
                level: 5,
                group: "dev"
            }
}

So here we see, there is no schema. So, we can add anything in the document, all document doesn’t have to look the same.

You may like the following MongoDB tutorial:

In this tutorial we learned what is MongoDB and also covers the below points:

  • How MongoDB works?
  • What is Database, Collection and Documents in MongoDB
  • Relational vs Non-Relational Databases
  • What is a Relational Database(RDBMS)
  • What is Non-Relational Database
  • Difference between RDBMS and MongoDB
  • Is MongoDB a Relational Database