MongoDB

A document database for flexible, evolving data shapes.

What is it?

MongoDB is a document database: instead of rows in tables with a fixed set of columns, data is stored as JSON-like documents grouped into collections. Two documents in the same collection don't need identical fields, which makes MongoDB a natural fit for data whose shape varies or evolves — logs, content with optional fields, or catalogs where different items have different attributes.

It contrasts directly with a relational database like PostgreSQL or MySQL, where every row in a table shares the same columns and relationships are enforced by the schema itself.


When to use it

MongoDB fits content with varied or nested structure — product catalogs with different attributes per category, user-generated content, or event/log data — where forcing everything into uniform rows and columns would mean constant schema migrations. It's less of a natural fit for data with lots of relationships between entities (orders linked to users linked to products), where a relational database's joins and constraints do a lot of the correctness work for you. For most beginner projects with structured data — users, orders, posts — MySQL or SQL fundamentals are the better place to start, since they transfer to almost every other database too.


Core concepts
  • Documents — the basic unit of data, a JSON-like object (technically BSON) with whatever fields it needs.
  • Collections — a group of documents, roughly analogous to a table, but without a fixed schema.
  • Flexible schema — you can add a new field to new documents without an ALTER TABLE-style migration affecting existing ones.
  • Embedding vs. referencing — related data can be nested directly inside a document (fast reads, some duplication) or referenced by ID across collections (like a foreign key, but without enforced constraints).
  • Aggregation pipeline — MongoDB's query language for filtering, grouping, and transforming data, conceptually similar to SQL's GROUP BY and joins but expressed as a pipeline of stages.

A taste of the code
const doc = await db.collection("posts").insertOne({
  title: "Hello Mongo",
  tags: ["intro", "database"],
  author: { name: "Ada", verified: true },
});

const posts = await db
  .collection("posts")
  .find({ tags: "intro" })
  .toArray();

Notice the nested author object and the tags array living directly inside the document — no separate "authors" or "tags" table required.


Your first steps
  1. Create a free cluster on MongoDB Atlas (the managed cloud service) instead of installing MongoDB locally — it's the fastest way to get a real connection string.
  2. In a Node.js project, install the official driver with npm install mongodb and connect using your Atlas connection string.
  3. Insert a handful of documents into a collection, then query them back with different filters.
  4. Model one-to-many data two ways — embedded and referenced — and notice how the trade-off between read speed and duplication shows up in each.
  5. Try Mongoose (a popular schema layer on top of the raw driver) once you want lightweight validation on otherwise flexible documents.

← Back to Stack Picker All primers