MongoDB
A document database that stores data as flexible, JSON-like documents instead of rows in fixed tables.
- Type
- Document database (NoSQL)
- Created by
- MongoDB, Inc. (Community Server under the SSPL)
- Stores
- JSON-like documents (BSON) in collections
- In my stack
- Data whose shape changes often
What it is
In MongoDB, a record is a document: an object with fields, nested objects and lists, much like the JSON your app already sends and receives. Documents live in collections, and two documents in the same collection don’t need the same fields. There is no schema to migrate before you store something new.
You query it with a JSON-style query language and aggregation pipelines rather than SQL. It scales out across several servers with sharding, and MongoDB Atlas runs it as a managed service on AWS, Azure or Google Cloud. It also supports indexes, multi-document transactions and schema validation when you want some rules back.
Why I use it
The data looks like the code. An object in the app is stored as is, nested lists included, with no mapping layer between tables and objects.
Shapes can change freely. When every form, product or event has different fields, adding a new kind doesn’t require a migration.
Fast to start. For a prototype or an MVP, there is less to design up front, and Atlas gives you a production-ready cluster in minutes.
Scales horizontally. Large volumes of documents can be split across servers when one machine is no longer enough.
Where I use it
PostgreSQL is my default, because most business data is relational and benefits from strict rules. I reach for MongoDB when the data really is a collection of independent documents with varying shapes: form builders where each client designs their own forms, product catalogues with very different attributes, event and log data, or when a client’s existing system already runs on it. With Node.js and TypeScript it feels natural, since data stays JSON from the database to the screen.
Best use cases
- Configurable forms and content, where each document can have its own structure.
- Catalogues with products whose attributes vary a lot from one category to another.
- Events, logs and IoT readings: high write volumes of self-contained records.
- Prototypes and MVPs whose data model is still changing every week.
When I’d use something else
When records are deeply linked, such as users, sites, inspections, actions and approvals, and the rules between them matter, a relational database like PostgreSQL protects the data better, and its jsonb columns already cover much of the flexibility. One more thing to keep in mind: like SQL, MongoDB queries can be injected if user input is passed in as an object, so input must be validated.
A small example
Two documents with different shapes in the same collection, and a query on a field only one of them has.
const forms = db.collection("form_responses");
// Two forms, two different shapes: no migration needed
await forms.insertMany([
{ formId: "visitor-check-in", name: "Sara", company: "Acme", badge: 42 },
{ formId: "toolbox-talk", topic: "Working at height", attendees: ["Ali", "Nora"] },
]);
// Every toolbox talk Nora attended
const talks = await forms
.find({ formId: "toolbox-talk", attendees: "Nora" })
.toArray();