The first step in using MongoDB is to understand the basic concepts of MongoDB and the data model (data structure), knowing how the data is stored. In fact, MongoDB is very similar to MySQL, so everyone who knows MySQL will not have any difficulty learning MongoDB.

Basic Concepts of MongoDB

Database

The MongoDB database is the same as the MySQL database.

Collection

A MongoDB collection refers to a collection of document data, similar to the concept of a table in MySQL.

Document

A MongoDB document, similar to a row of data in a MySQL table, is a JSON-structured data consisting of multiple fields, making the document structure in MongoDB very flexible.

Field

A MongoDB document field is similar to the concept of a field in an MySQL table, representing the specific data to be stored, with each field having its own data type.

Index

The MongoDB index is similar to the MySQL index, with the purpose of improving query efficiency.

Aggregation

The concept of MongoDB aggregation is similar to the Group by/Count/Sum aggregation analysis in MySQL, used for data statistical analysis.

Comparison of Concepts between MongoDB and MySQL

MYSQL MongoDB
Database Database
Table Collection
Row Document
Column Document Field
Index Index
Aggregation (e.g., group by) Aggregation

Basically, MongoDB's concepts can be matched with MySQL, so MongoDB is the most similar non-relational data to relational databases (NoSQL).

MongoDB Document Data

A document is simply a JSON structure comprised of multiple fields, each with its own data type. When developing with MongoDB, the focus is mostly on how to design the document structure, similar to how the core of using MySQL involves designing table structures.

Example of MongoDB document data:

{
  "_id": "5cf0029caff5056591b0ce7d",
  "firstname": "Jane",
  "lastname": "Wu",
  "address": {
    "street": "1 Circle Rd",
    "city": "Los Angeles",
    "state": "CA",
    "zip": "90404"
  },
  "hobbies": ["surfing", "coding"]
}

It can be any arbitrarily nested JSON structure, something that MySQL table structures cannot achieve. The _id field serves as the document's primary key, and if you do not specify a specific value, MongoDB will generate a unique value at random.

The biggest difference between MongoDB's data structure and MySQL tables is that MongoDB does not require predefining the document structure. With MongoDB, you can directly insert JSON data into a collection without any need for predefined schema. Each time you write JSON data into a MongoDB collection, the format can be different, allowing fields to be added or removed at will.

Note: Although MongoDB's document structure is quite flexible and can accommodate varying data formats for each entry, in practical applications, the data structure within a collection is usually uniform. Otherwise, if each entry's format is different within the collection, maintaining it will be a nightmare as you won't know the format of the data retrieved each time.