Understanding NoSQL Databases

NoSQL is a broad category of database technologies that do not rely exclusively on the traditional relational table model. The term does not mean that these systems cannot perform queries, use schemas, or support transactions.

Common NoSQL categories include:

  • Document databases: Store structured documents. MongoDB is an example.
  • Key-value databases: Store values addressed by keys. Redis is a common example.
  • Wide-column databases: Organize data using column families and are designed for particular distributed workloads. Apache Cassandra is an example.
  • Graph databases: Represent entities and relationships as nodes and edges. Neo4j is an example.

These categories overlap in some capabilities, but they differ in their data models, query approaches, consistency characteristics, and typical access patterns.

Relational databases versus document databases

Relational databases organize data into tables with defined columns and relationships. Document databases organize data into documents and collections.

For example, an online store may represent a customer and their addresses in separate relational tables. A MongoDB implementation might embed a small set of addresses within a customer document if they are generally accessed together.

Neither representation is automatically better. The appropriate model depends on the expected queries, update frequency, cardinality, integrity requirements, and growth patterns.

SQL and MongoDB query syntax

MongoDB uses a document-oriented query interface rather than SQL as its primary query language.

A relational query might look like this:

SELECT name, age
FROM users
WHERE age >= 25;

The equivalent MongoDB query is:

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

The filter selects users whose age is at least 25. The projection returns only the name and age fields while excluding _id.

MongoDB also provides aggregation pipelines for grouping, transforming, joining, and analyzing data. The aggregation framework will be covered in depth in a later chapter.

Database model comparison
Relational: Tables and relationships
Useful for structured data, relational integrity, and workloads that benefit from SQL and relational operations.
Document: Documents and collections
Useful when application data maps naturally to documents and nested structures.
Key-value: Keys and values
Useful for fast key-based access patterns, caching, and related workloads.
Graph: Nodes and edges
Useful when traversing relationships is central to the application.
Key Takeaways
  • NoSQL describes multiple database models rather than a single technology.
  • MongoDB belongs to the document-database category.
  • Relational and document databases both support querying and can support transactions.
  • MongoDB queries use document-shaped filters and projections.
  • Choose a database according to the data model and workload.
Knowledge Check

Which database category does MongoDB primarily belong to?