Skip to content
Databasesbeginner

What is MongoDB?

Updated Nov 30, 20263 min readBased on official documentation
What is MongoDB?

In short

MongoDB is a document database. It stores data as flexible, JSON-like documents instead of rigid rows and tables, which makes it easy to match the data shape your application code already uses. It also offers transactions, replication, and horizontal scaling through sharding.

MongoDB is a database for applications that work with data in flexible shapes. Developers use it to store, search, and update information. If you have worked with relational databases like PostgreSQL or MySQL, MongoDB will feel different, but the core job is the same: keep data safe and make it easy to read back.

How it stores data

MongoDB keeps records as documents. A document is a group of field and value pairs. It looks a lot like a JSON object, which is a common text format for structured data. A field's value can be a single number or text, another document, or an array.

Documents live in collections. A collection is roughly the MongoDB version of a table, but it does not force every record to have the same fields. Two documents in one collection can have different shapes. MongoDB also supports read-only views, which present data from a collection in a different form.

Here is a simple example of one document:

{ _id: ObjectId("507f1f77bcf86cd799439011"), name: "Alice", birthdate: ISODate("1990-01-01T00:00:00Z"), address: { street: "123 Main St", city: "Springfield", state: "IL" }, hobbies: ["reading", "hiking", "coding"] }

The address is nested inside the user record, and the hobbies are stored as a list. In a relational database, the same data would often be split across several tables and joined back together when read. With MongoDB, the related data can sit in one document, which is faster to read in many cases.

Why developers pick it

The main appeal is the match with application code. Many programming languages already represent data as objects or dictionaries, so the stored document maps closely to what your program holds in memory. This reduces the amount of translation code you need, which is often called object-relational mapping in the relational world.

The flexible structure also helps when your data model changes. You can add a new field to newer documents without stopping the database or migrating every existing record first. This is useful early in a project, when requirements shift often.

Transactions, replication and scaling

MongoDB supports multi-document transactions. A transaction groups several read and write operations so they succeed together or not at all. The documentation describes these transactions as following ACID rules, which cover atomicity, consistency, isolation, and durability. Transactions can also span sharded clusters.

Replication copies your data across several servers. One server is the primary and accepts writes. If it becomes unavailable, the cluster elects a new primary automatically, so writes can continue. Extra copies also add data redundancy and can increase read capacity.

For very large datasets, MongoDB uses sharding. Sharding splits data across machines based on a shard key, which is a field you choose to decide where each document belongs. This is how horizontal scaling works: you add more machines rather than a bigger single machine. Zone sharding can keep certain documents on servers in a chosen region, and the shard key can be refined later as your application changes.

Common mistakes

Beginners often design a MongoDB schema as if it were a set of relational tables. Flexible schemas do not remove the need for planning. Think about which data is read together and how it will grow. Embedding everything in one document can make documents very large, while splitting too much can bring back the extra lookups you wanted to avoid.

Another common mistake is choosing a poor shard key. The shard key affects how evenly data spreads across machines. Pick it with your most common queries in mind, because changing it later is more work.

When it fits and when it may not

MongoDB suits applications with varied or changing data, such as content platforms, catalogs, and user profiles. It is also a good fit when you want to scale out across many servers. If your data is highly structured, has many relationships, and needs complex joins across tables, a relational database may be a simpler choice. Many teams use both, each for the work it does best.

To learn the details, start with the official introduction in the MongoDB manual and then try a few queries on a small collection.

FAQ

Is MongoDB a relational database?

No. MongoDB is a document database. It stores flexible, JSON-like documents in collections instead of fixed rows in tables.

Does MongoDB need a fixed schema?

No. Collections do not enforce a rigid, pre-defined schema, so documents in the same collection can have different fields. You still benefit from planning your data model carefully.

Does MongoDB support transactions?

Yes. Multi-document transactions let you run several read and write operations as one all-or-nothing unit, including across sharded clusters.

What is sharding in MongoDB?

Sharding splits data across multiple machines based on a shard key. It is MongoDB's way of scaling horizontally as data and traffic grow.

Sources

This page is based on the official documentation below. Read it for full detail.

MongoDB — official documentation ↗
blozhikDeveloper news from across the web, explained. Every story links to its original sources.