Data engineering › Databases
What is MongoDB? A gentle guide to document databases
What MongoDB is, what it means to store data as documents, how it differs from SQL databases, when it fits, and how to try it yourself in a few minutes.
When you start learning development, the name MongoDB tends to show up sooner than you’d expect. It appears in web development courses, and it sits in the tech stacks of plenty of job postings. But if someone asks what MongoDB actually is, perhaps “a NoSQL database” is the only answer that comes to mind.
MongoDB is a database that stores data as documents that look a lot like JSON. Relational databases such as MySQL or PostgreSQL split data across several tables and join it back together when you need it. MongoDB, on the other hand, tends to keep related information together in a single document. That’s why you can store the objects your program works with and read them back almost as they are. The name comes from the word “humongous,” and MongoDB first came out in 2009 (MongoDB’s story).
What it means to store documents
Say you’re keeping track of the books in a small bookshop. In MongoDB, each book becomes one document, shaped like this.
{
_id: ObjectId('6ae016b83654c79cea284d0c'),
title: "The Little Prince",
author: "Antoine de Saint-Exupéry",
year: 1943,
tags: ["classic", "fable"],
stock: { store: 3, warehouse: 12 },
illustrated: true
}
If you’ve seen JSON before, this probably reads naturally. Each name and value pair is called a field. A value can be a number or a string, but it can also be an array like tags, or another document nested inside, like stock. In a relational database, the tags and the stock counts would likely live in separate tables. Here they sit inside the book itself.
_id is a unique value that every document must have, and it plays the role of a primary key. If you don’t set one, an ObjectId is generated for you. And although it looks like JSON, the document is actually stored in a binary format called BSON. BSON adds types such as dates and ObjectId to what JSON offers. So you can store a date as a date, without turning it into a string first. One limit is worth remembering, though: a single document can’t be larger than 16 MB (limits).
A group of documents is called a collection, and a database holds one or more collections. The documents in a collection don’t have to share the same fields. It’s perfectly fine for only some books to have a field like illustrated.
How it differs from a relational database
If you learned relational databases first, the quickest way in is to pair up the terms. Here are the ones you’ll use most, taken from MongoDB’s SQL to MongoDB Mapping Chart.
| Relational database | MongoDB |
|---|---|
| Table | Collection |
| Row | Document |
| Column | Field |
| Primary key | The _id field |
| Join | Embedding, or $lookup |
The bigger difference, though, is in how you divide the data. A relational database splits data into tables so the same information isn’t repeated in several places, then joins the tables back together when you read. MongoDB suggests that data you read together should be stored together. Its data modeling guide says to embed data that is queried and updated together, and to keep data that grows without bounds, or that can exist without its parent, in a separate place and reference it. For example, a book and its tags would go in one document, while reader reviews, which keep piling up for a popular book, would go in their own collection.
The schema is handled differently too. In a relational database, you define the columns before you can insert anything. In MongoDB, you can insert documents without defining a structure first. Of course, letting any shape in isn’t always a good thing. That’s why MongoDB also offers schema validation with $jsonSchema, which can require certain fields and types.
Perhaps you’ve also heard that MongoDB doesn’t have transactions. That’s an old story. Writes to a single document have always been atomic, and multi-document ACID transactions have been available on replica sets since 4.0 and on sharded clusters since 4.2. Both are ways of running MongoDB on several servers, and I’ll come back to them briefly below. Still, the transactions page notes that they cost more than single-document writes, so in most cases a well-designed document is the better answer.
Trying it yourself
Explanations only go so far, so let’s try it out. If you have Docker installed, the image from MongoDB’s installation guide will get a server running in a few minutes. The first line starts the server in the background, and the second opens mongosh, the shell that ships inside the same container.
docker run --name mongodb -p 27017:27017 -d mongodb/mongodb-community-server:9.0-ubi9-slim
docker exec -it mongodb mongosh
Once mongosh is open, let’s add three books. You don’t need to create the database or the collection first. They’re created the moment you insert data.
use bookshop
db.books.insertMany([
{ title: "The Little Prince", author: "Antoine de Saint-Exupéry", year: 1943,
tags: ["classic", "fable"], stock: { store: 3, warehouse: 12 }, illustrated: true },
{ title: "Frankenstein", author: "Mary Shelley", year: 1818,
tags: ["classic", "gothic"], stock: { store: 0, warehouse: 5 } },
{ title: "Project Hail Mary", author: "Andy Weir", year: 2021,
tags: ["science-fiction"], stock: { store: 7, warehouse: 2 } }
])
{
acknowledged: true,
insertedIds: {
'0': ObjectId('6ae016b83654c79cea284d0c'),
'1': ObjectId('6ae016b83654c79cea284d0d'),
'2': ObjectId('6ae016b83654c79cea284d0e')
}
}
None of the books had an _id, yet each one got an ObjectId. These values change on every run, so you’ll see different ones when you try it. Now let’s search with a condition. The first argument to find() is the condition, and the second picks which fields to show.
db.books.find({ tags: "classic" }, { _id: 0, title: 1, year: 1 })
db.books.find({ "stock.store": { $gt: 0 } }, { _id: 0, title: 1, "stock.store": 1 })
[
{ title: 'The Little Prince', year: 1943 },
{ title: 'Frankenstein', year: 1818 }
]
[
{ title: 'The Little Prince', stock: { store: 3 } },
{ title: 'Project Hail Mary', stock: { store: 7 } }
]
The first query found books with the single value "classic", even though tags is an array. When you query an array field with a value, MongoDB matches the documents whose array contains it. The second query used a dot to step into the nested document and picked the books with more than zero copies in the store. Names that start with $, like $gt for “greater than,” are operators that express conditions.
Last comes an update. Say four new copies of Frankenstein arrive at the store.
db.books.updateOne({ title: "Frankenstein" }, { $inc: { "stock.store": 4 } })
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
matchedCount is the number of documents that matched the condition, and modifiedCount is the number that actually changed. Since an operation on a single document is atomic, $inc saves you from writing code that reads the value, adds to it, and writes it back.
When it fits and when it doesn’t
MongoDB tends to fit well when your data comes in varied shapes or changes often. Product catalogs where each item has different attributes, content like posts and comments, user profiles, and the event logs an app collects are the usual examples. It also suits work where you read and write whole objects at a time. And when the data grows, sharding lets you spread it across several servers, while a replica set lets another server take over automatically if the primary goes down.
On the other hand, if many kinds of data are tightly connected and you need to combine them in different ways all the time, a relational database may feel easier. Accounting data, where the relationships and constraints between tables are central, is one example. Analytics with lots of free-form joins across tables is another. Of course, MongoDB has $lookup, and its aggregation pipeline filters and groups data through a series of stages, so these things are possible. The starting point of the design is simply different, and that’s worth keeping in mind. So before choosing, I’d suggest writing down what shape your data has and how it’s mostly read.
Other names you’ll hear
As you learn MongoDB, you’ll often run into the name Atlas. MongoDB Atlas is a cloud database service run by the company that makes MongoDB. You can create a cluster from the web without installing or managing servers, and there’s a free cluster with 512 MB of storage that works well for practice (pricing).
The license is worth a quick look too. Community Server, the free download, is distributed under the SSPL (Server Side Public License) for versions released after October 16, 2018, and earlier versions use the AGPL v3.0 (licensing). That’s why, if you plan to ship MongoDB as part of a product or service, it’s a good idea to read the license terms first.
Where to start
If you’re new to it, start the server with the Docker commands above and follow the official CRUD guide to insert, find, update, and delete a few documents. If installing feels like a hassle, an Atlas free cluster works too. The current stable release is the 9.0 series, which came out on September 28, 2026 (release notes), and I ran these examples on October 27, 2026 with MongoDB 9.0.4 and mongosh 2.6.0.
AI tools helped with the research for this post.