What is MongoDB?


MongoDB is an open source database management system.

The difference between MongoDB and relational database management systems (RELATIONAL DATABASE MANAGEMENT SYSTEM – RDBMS) such as MySQL, MS SQL … is that storing data in NoSQL format means that there are no constraints between tables.

MongoDB is a document database (a document-oriented database).

Data in MongoDB is saved as json. Therefore, document database can be understood here means that data is stored in text / json, not in a table, column type with byte, clob data type … like in RDBMS.

Some concepts in MongoDB

When working with MongoDB you will encounter the following concepts:

Collection, Document, Embeded documents, linking, Primary Key. So what are they?

Database Database
Table Collection
Row Document
Column Field
Table Join Embedded Documents
Primary Key Primary Key (default is _id)

Surely you will wonder why not always use the concept of table, row … to make it easy to remember and set what Collection, document is. There is a reason for it.

  • Collection : Collection is a group of data of the same type specified by the user, the difference between the collection and the table in RDBMS is that the collection is not bound, related to another collection. Each data in the collection is not fixed on the number of fields like row in the table.
  • Document : Document is a document stored in json format. It is equivalent to row in RDBMS table, but the data in document is quite flexible, it’s not fixed how many fields, data in each field must be fixed.
  • Embedded Documents : If in RDBMS, we have type of join tables, for example the table person joins the address table to know what addresses a person has then in MongoDB, the person’s document will contain an array of document addresses.
  • Primary Key : By default, the primary key in MongoDB is the _id field. When inserting, if you do not specify the _id field, it will automatically generate the value for _id.

You see, the data in Collection, Document is different from Table, Row.

It is very flexible, you can insert anything you want, not just a fixed type, the number of fields is fixed.

For example:

Here is a document in MongoDB:

    "_id" : ObjectId("5b5ddca8e25fd6f9fb473087"),
    "name" : "kai",
    "age" : 27,
    "address" : [ 

If it is in RDBMS, we will have 2 tables:

  • table person (id, name, age)
  • table address: (id, descript, person) where person links to the table person.


But when the structure of the data changes, you will have to edit the column, edit the table … and MongoDB does not.

For example, the adress has more information such as province, street … column age changes to text, or adding a new column is gender, with MongoDB we just need to add data and insert normally:

    "_id" : ObjectId("5b5ddeb7e25fd6f9fb4730c6"),
    "name" : "kai",
    "age" : "27",
    "address" : [ 
            "country" : "viet nam",
            "province" : "ha noi",
            "street" : "nguyen trai"
            "country" : "korea",
            "province" : "seoul"
    "gender" : "male"

MongoDB advantages and disadvantages

The pros and cons of MongoDB I separate a separate article, because I want to explain in more detail.

Read here: MongoDB advantages and disadvantages, when to use MongoDB