Blogs · SQL · Database

SQL: Going Into Applications With MySQL and MongoDB

A practical comparison of relational and document databases in application development, with notes on schemas, queries, transactions, and data modeling.

2020.09.04 · 1 min read · by Zhenlin Wang

Introduction

Application databases should be chosen by access pattern. MySQL and MongoDB represent two common choices: a relational database and a document database.

Neither is universally better. Each fits different modeling and operational needs.

MySQL

MySQL stores data in relational tables.

Use it when:

Example:

SELECT users.id, users.email, orders.total_amount
FROM users
JOIN orders
  ON users.id = orders.user_id
WHERE orders.created_at >= '2024-01-01';

Relational modeling encourages normalized tables and explicit relationships.

MongoDB

MongoDB stores document records, usually JSON-like documents.

Use it when:

Example document:

{
  "user_id": "u_123",
  "email": "ada@example.com",
  "settings": {
    "theme": "dark",
    "language": "en"
  }
}

Document modeling often embeds data that would be joined in a relational design.

Data Modeling Difference

In relational databases, ask:

What entities exist, and how do they relate?

In document databases, ask:

What document shape does the application need to read and write?

If the app constantly reads a user and their settings together, a document can be convenient. If the app needs flexible joins across many entities, relational design is often cleaner.

Transactions and Consistency

Relational databases are strong defaults for transactional workloads. MongoDB also supports transactions, but document databases are often best when the data model avoids frequent cross-document transactions.

If the system handles money, inventory, or strong consistency requirements, start with a relational design unless there is a clear reason not to.

Choosing Between Them

Use MySQL when:

Use MongoDB when:

The best database is the one whose data model matches the application, not the one with the most fashionable label.