What Is MongoDB? β Definition and How It Works
Learn what a document database is, how MongoDB's model and indexes work, where flexible schemas help and hurt, and when to choose it.
MongoDB is a document database. Records are JSON-like documents grouped into collections, and documents in one collection need not share the same fields.
1. The Document Model
{
"_id": "A-4192",
"title": "Widget",
"price": { "amount": 49.90, "currency": "USD" },
"tags": ["search", "tools"],
"collected_at": "2026-08-18T09:00:00Z"
}
Nested objects and arrays are stored as they are, so a record with a list of tags needs no join table. That maps directly onto how collected data usually arrives.
2. Flexible Schema Cuts Both Ways
Helps: sources with varying fields land without migration, and adding a field requires no coordination.
Hurts: nothing prevents inconsistency, so a field can be a string in some documents and a number in others β and the problem surfaces at query time, far from its cause.
Schema validation rules exist and are worth using precisely because the default permits drift.
3. Indexes Decide Performance
Without an index, a query scans the collection. With one, it seeks.
Compound indexes serve queries that filter on several fields, and their field order matters β an index on (source, collected_at) serves a query filtering on source, and does not serve one filtering only on time.
4. Aggregation
The aggregation pipeline transforms documents through stages β matching, grouping, projecting, unwinding arrays, joining to another collection.
It handles the analytical work that would otherwise need exporting, and its stage ordering matters: filtering early reduces what later stages process.
5. Where It Fits for Collected Data
- Varying shapes across sources, without a migration per difference.
- Nested records stored as extracted.
- Rapid iteration, where the schema is still settling.
- High write throughput with straightforward horizontal scaling.
6. Where a Relational Store Fits Better
- Well-understood, stable schemas.
- Complex joins across entities.
- Multi-table transactions, though modern versions do support transactions.
- Ad-hoc analytical SQL, which the ecosystem overwhelmingly expects.
7. Practical Notes
- Use a stable
_idderived from the source identifier so re-runs upsert rather than duplicate. - Add schema validation once the shape settles.
- Index for your actual queries, not speculatively; every index costs write throughput.
- Beware unbounded arrays β documents have a size limit, and a growing array eventually reaches it.
- Store timestamps as dates, not strings, so range queries work.
8. Summary
MongoDB stores JSON-like documents with flexible schemas, nested structures, indexes, and an aggregation pipeline for analysis.
Its flexibility suits collected data whose shape varies and evolves; that same flexibility permits inconsistency, so schema validation, deliberate indexes, and stable identifiers are what keep a collection usable as it grows.