MongoDB introduction

MongoDB introduction

MongoDB is an open source NoSQL document oriented database. Every thing in MongoDB is based on managing documents. Data or records are stored in documents that use JSON like objects. So in general MongoDB contains multiple databases, each database contains multiple collections (tables in RDBMS) and each collection contains multiple documents (rows in RDBMS).

MongoDB Terminology

Collection

A Collection is a group of documents. A collection represent a table in an RDBMS.

Document

A Document is a BSON document presented as a set of key-value pairs. Documents have dynamic schema which means that document can contains different fields. Every document is represented by a unique _id field which is the Primary Key.

BSON

BSON extends the JSON model to provide additional data types and to be efficient for encoding and decoding within different languages. BSON is a binary encoded format for JSON data.

MongoDB Diagram

RDBMS Terminology in MongoDB

RDBMS MongoDB
Table Collection
Row Document
Column Field
Joins Embedded Documents

Advantages of MongoDB over RDBMS

There are a lot of advantages of using MongoDB, we can note:

  • No schema required
  • Scalablility of documents
  • No complex joins
  • Replication and high availability
  • Big Data
  • Auto-sharding

Document example in MongoDB

[
  {
    "id": ObjectId("5e2f3e9a639f39df1eb90eed"),
    "name": "product 1",
    "ref": 50,
    "date": new Date("2019/09/10"),
    "subCategories": [
      {
        "ref": 3,
        "name": "sub1"
      },
      {
        "ref": 4,
        "name": "sub1"
      }
    ],
    "add":{
        "meta": "off",
        "key": "val"
    }
  },
  {
    "id": ObjectId("5e2f3e9a639f39df1eb90eee"),
    "name": "product 2",
    "ref": 25,
    "date": new Date("2020/08/08"),
    "subCategories": [
      {
        "ref": 6,
        "name": "sub2"
      }
    ]
  }
]

Requirements

  • MongoDB server installation
  • Some knowledge about JSON objects