No history yet

Introduction to MongoDB

Beyond Tables and Rows

For a long time, databases were like spreadsheets. Data was organized neatly into tables with rows and columns, a system known as a relational database (or SQL database). This structure is rigid. If you want to add a new type of information, you often have to change the entire table structure.

MongoDB offers a different approach. It's a type of NoSQL database, which stands for "Not Only SQL." Instead of enforcing a strict structure, it's designed for flexibility, speed, and handling large amounts of varied data.

MongoDB is a NoSQL database that stores data in flexible, JSON-like documents.

This flexibility makes it a popular choice for modern applications where the data might not fit a predefined mold or could change over time.

Documents and Collections

Instead of rows and tables, MongoDB uses documents and collections. A document is the basic unit of data. It's a set of key-value pairs, similar to a JSON object. A document can hold various types of data, including numbers, strings, arrays, and even other documents nested inside.

{
  "_id": "user123",
  "username": "alex_smith",
  "email": "alex@example.com",
  "interests": ["hiking", "photography", "cooking"],
  "address": {
    "street": "123 Maple St",
    "city": "Anytown"
  }
}

A group of documents is stored in a collection. You can think of a collection as being roughly equivalent to a table in a relational database. However, unlike a table, a collection doesn't require its documents to have the same structure. One document might have an email address, while another in the same collection might not. This schemaless design is a core feature of MongoDB's flexibility.

Key Features

MongoDB isn't just flexible; it's also built for performance and reliability. Three key features make this possible: high performance, high availability, and scalability.

High performance comes from its ability to store related data together in a single document. Instead of joining multiple tables to retrieve information, an application can often fetch everything it needs in a single query, which is much faster.

High availability is achieved through a process called replication. MongoDB can maintain multiple copies of your data across different servers. If one server fails, another one can take over immediately, ensuring your application stays online. This setup is called a replica set.

Finally, MongoDB provides horizontal scalability through sharding. As an application grows, a single server might not be enough to handle all the data or traffic. Sharding solves this by splitting a large collection across multiple servers, or "shards." Each shard holds a portion of the data, allowing the database to handle much larger datasets and higher traffic loads by simply adding more servers.

These features combined make MongoDB a powerful and robust database for applications that need to grow and adapt.

Time to check your understanding of these core concepts.

Quiz Questions 1/5

What is the fundamental difference between how MongoDB organizes data compared to a traditional relational (SQL) database?

Quiz Questions 2/5

In MongoDB, a group of documents is stored in a:

Understanding these foundational ideas is the first step to working effectively with this flexible and powerful database.