Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL vs NoSQL (MongoDB) #272

Open
qingquan-li opened this issue Sep 18, 2024 · 0 comments
Open

SQL vs NoSQL (MongoDB) #272

qingquan-li opened this issue Sep 18, 2024 · 0 comments
Labels

Comments

@qingquan-li
Copy link
Owner

qingquan-li commented Sep 18, 2024

Why Database?

Common website architecture:

Frontend (user interface) - Backend (API, database...)

UI (HTML, CSS, JavaScript) - API (Java) - Database (MongoDB)

Similar examples from the real world: Restaurant - Waiter - Kitchen

What is SQL (Relational Database)?

Popular SQL Databases

PostgreSQL, MySQL

Structure

SQL databases are organized into tables with predefined schemas.

id name age email
123 John Doe 28 [email protected]
124 Jake Lee 18 [email protected]

Query Language

SQL (Structured Query Language) is used to interact with relational databases.

To find all users who are over 25 years old:

SELECT name, age
FROM users
WHERE age > 25;

Result (SQL):

name age
John Doe 28

What is NoSQL (Non-relational Database)?

Popular NoSQL databases

MongoDB, Redis

Structure

NoSQL databases are more flexible and can store data in a variety of formats like documents (e.g., MongoDB) or key-value pairs (e.g., Redis).

Document Structure (NoSQL): In MongoDB, data is stored in documents, which are similar to JSON objects. The collection name is users, and each document looks like this:

{
    "_id": ObjectId("..."),
    "name": "John Doe",
    "age": 28,
    "email": "[email protected]"
}

Querying Data (MongoDB)

To find all users who are over 25 years old:

db.users.find({ age: { $gt: 25 } }, { name: 1, age: 1, email: 0, _id: 0 });

Result (MongoDB):

[
    { "name": "John Doe", "age": 28 }
]

Key Differences

Dimension SQL (PostgreSQL) NoSQL (MongoDB)
Data Model Relational (Tables with rows and columns) Document-based (JSON-like documents)
Schema Fixed schema (must define tables and columns upfront) Flexible schema (can change on the fly)
Data Relationships Well-suited for structured data and complex relationships (via joins) Embedding documents or referencing documents, no native joins
ACID Transactions Fully ACID-compliant (Atomicity, Consistency, Isolation, Durability) Partial ACID support (starting from version 4.0 for multi-document transactions)
Scaling Vertical scaling (increase hardware capacity) Horizontal scaling (scale by adding more servers)
Write Speed Generally slower due to strict ACID compliance and schema validation Generally faster due to flexible schema and BASE properties
Read Speed Optimized for complex queries and joins Faster for simple queries, but slower for complex queries involving references
Use Case Suitable for structured data and complex queries. Ex: banking systems, financial transactions, inventory management. Suitable for unstructured data and large, distributed datasets. Ex: social media, real-time data processing, content management systems.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant