...

/

Introducing MongoDB and Its Architecture

Introducing MongoDB and Its Architecture

Let's review MongoDB as a leading NoSQL database, explain its core features, and highlight common use cases.

What is MongoDB?

MongoDB is a popular, open-source database designed for high performance, high availability, and easy scalability. Unlike traditional databases that store data in tables and rows, MongoDB stores data in flexible, JSON-like documents.

Press + to interact

MongoDB is one of the most widely used NoSQL databases. It is especially popular for web and mobile applications, real-time analytics, content management systems, and Internet of Things (IoT) applications.

Why should you choose MongoDB?

MongoDB is designed to handle a wide variety of data types and workloads, making it a great choice for many modern applications. It allows applications to grow without major architectural changes, as it adapts easily to changing data requirements. It supports a large community, extensive documentation, and many integrations.

Key features of MongoDB

Having learned about the core advantages of NoSQL databases already, let's take a look at the key features of MongoDB.

  • Dynamic data structures: Unlike rigid table schemas, MongoDB collections can hold documents with varying fields and data types, allowing the data model to evolve as the application grows.

  • Effortless scalability: Built with distributed systems in mind, MongoDB can automatically spread data across multiple servers, supporting applications that need to handle increasing loads or global reach.

  • Expressive queries and indexing: MongoDB offers a powerful query language and supports a variety of indexes, enabling developers to efficiently search, filter, and analyze the data, even as it grows in size and complexity.

  • Seamless integration with modern development: With its JSON-like document format and drivers for all major programming languages, MongoDB fits naturally into today’s development workflows and technology stacks.

MongoDB's data structure

As repeatedly mentioned, MongoDB organizes data differently from traditional relational databases. Instead of tables and rows, MongoDB uses documents and collections.

What is a document?

A document is the basic unit of data in MongoDB. Similar to JSON objects, documents store data as key-value pairs. They can store different types of data, including strings, numbers, arrays, and even nested documents. 

Here’s a basic sample of how data is stored in MongoDB:

{
"_id": "63f2a45d5c8f9a1e4a6b987e",
"name": "Alice",
"age": 25,
"email": "alice@example.com"
}
Basic example of a MongoDB document

Here, the curly braces ({}) indicate that this is a document. Each piece of information is stored as a key-value pair, e.g., "name": "Alice". Also, every document has a unique identifier, _id (shown at line 2), that uniquely identifies each document and is never repeatedValue that uniquely identifies a document and is never repeated.

Note: In MongoDB, the primary keyA key that uniquely identify each document must be named _id, which is a 12-byte hexadecimal value by default. We can still have other fields named id or anything else in our document.

What is a collection?

A collection is a group of MongoDB documents. It's similar to a table in SQL databases, but collections do not enforce a fixed schema. All documents in a collection are stored together, but they can have different structures.

For example, suppose we have a collection called person:

[
{
"_id": "63f2a45d5c8f9a1e4a6b987e",
"name": "Alice",
"age": 25,
"email": "alice@example.com"
},
{
"_id": "63f2a45d5c8f9a1e4a6b987f",
"name": "Bob",
"age": 30,
"email": "bob@example.com",
"address": {
"street": "123 Main St",
"city": "Springfield"
}
},
{
"_id": "63f2a45d5c8f9a1e4a6b9880",
"name": "Charlie",
"skills": ["JavaScript", "Python"],
"experience": 5
}
]
Basic example of a MongoDB collection

It contains three documents (each enclosed in {}). Each document has a unique _id field. Documents in this collection have different structures:

  • The first document (lines 2­–7) has a basic structure (name, age, email).

  • The second document (lines 8­–17) has an additional address field (nested document).

  • The third document (lines 18­–23) has a skills array and an experience field, and no age or email.

What is a database?

A database in MongoDB is a container for collections. We can have multiple databases on a single MongoDB server, each storing collections for different applications or purposes.

Press + to interact
Database as a large box containing several folders
Database as a large box containing several folders

Each database is independent and has its own collections and documents. A common practice is to use one database per application. For example, our database can be organized as:

myAppDatabase
├── users (collection)
├── products (collection)
└── orders (collection)
A sample hierarchy

Here’s a quick recap of the databases' hierarchy:

Level

MongoDB term

Description

SQL equivalent

Top

Database

Container for collections

Database

Middle

Collection

Group of related documents

Table

Bottom

Document

Individual data entry (JSON-like)

Row/Record

In short, MongoDB's architecture—comprising databases, collections, and documents—offers a flexible and scalable way to organize data. This structure makes it easy to handle complex and evolving data models.