Skip to content

NoSQL Overview

Document, 键-值, wide-列, and 图数据库 — 当到 look 超出 SQL.

你将学到

  • 什么 NoSQL 平均值 and 为什么它 emerged
  • four 主要 NoSQL 类型: document, 键-值, wide-列, 图
  • 当 NoSQL 是 better fit than relational 数据库
  • CAP theorem and trade-off 之间 consistency and availability
  • Popular NoSQL 数据库 and 它们的 use 情况

概念讲解

什么是 NoSQL?

NoSQL ("Not Only SQL") refers to databases that do not use the relational table model. They emerged in the 2000s to address needs that relational databases struggled with: massive horizontal scale, flexible/unstructured data, and rapid schema evolution. NoSQL databases trade some guarantees (like strict consistency and joins) for flexibility, scale, or performance.

The Four Types

| Type | Example | Data model | Best for | |------|---------|------------|----------| | Document | MongoDB, CouchDB | JSON-like documents | Content management, catalogs, flexible schemas | | Key-Value | Redis, DynamoDB | Key → value pairs | Caching, sessions, real-time lookups | | Wide-Column | Cassandra, HBase | Column families | Time-series, heavy writes, large scale | | Graph | Neo4j, Amazon Neptune | Nodes and edges | Social networks, recommendations, fraud detection |

When to Choose NoSQL

NoSQL is a good fit when: your data is naturally hierarchical or unstructured (JSON documents), your schema evolves rapidly, you need horizontal scale across many servers, or you have extremely high write throughput. Relational databases are better when: your data is highly structured, you need complex joins and transactions, or strong consistency is non-negotiable.

The CAP Theorem

The CAP theorem states that a distributed database can provide at most two of three guarantees: Consistency (every read sees the latest write), Availability (every request gets a response), and Partition tolerance (the system works despite network failures). Since partitions are inevitable in distributed systems, the real choice is CP (consistency + partition tolerance) vs AP (availability + partition tolerance). Most NoSQL databases choose AP; relational databases traditionally choose CP.

BASE vs ACID

NoSQL databases often follow the BASE model: Basically Available, Soft state, Eventually consistent. This contrasts with ACID — instead of immediate consistency, the database converges to consistency over time. This is acceptable for many use cases (social feeds, product catalogs) but not for financial transactions.

示例代码

              -- MongoDB: insert a flexible document (no fixed schema)
db.users.insertOne({
  name: "Ada Lovelace",
  email: "[email protected]",
  roles: ["admin", "editor"],
  profile: {
    bio: "Mathematician and writer",
    skills: ["math", "logic"],
    location: "London"
  },
  createdAt: new Date()
});

-- Redis: set and get a key-value pair
SET session:abc123 "user_id=42"
EX 3600  -- expire in 1 hour

GET session:abc123

-- Cassandra: a wide-column insert
INSERT INTO events (event_id, user_id, event_type, timestamp)
VALUES (uuid(), 42, 'page_view', toTimestamp(now()));
            

Three NoSQL 模式: MongoDB document insert 使用 nested 数据, Redis 键-值使用 TTL, and Cassandra wide-列 insert.

动手试一试

常见错误

错误写法

选择 NoSQL 只是因为它 是 trendy

正确做法

开始使用 relational 数据库. 选择 NoSQL 仅当 你有 具体需要那个 SQL cannot meet: flexible Schema, massive horizontal scale, or 特定数据 model (图, 时间-级数).

错误写法

Assuming NoSQL 平均值否 Schema at 所有

正确做法

偶数 document 数据库 benefit 从 Schema — for validation, indexing, and code clarity. Use JSON Schema or 数据库-level validators 到 enforce 结构.

练习 NoSQL Overview

3 个练习

练习 NoSQL Overview

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误