80%
DefinitionMongoDB is a document database: JSON-like documents (BSON) in collections, with a flexible schema, rich queries, aggregation pipelines and horizontal scaling (sharding).
database: database
table: collection
row: document
column: field
data is read together (order + items): data is shared by many documents (a product)
one-to-few, bounded size: one-to-many unbounded (comments, logs)
the child has no life of its own: the child is queried on its own
"Design for your queries": model documents around how the app reads them.
Document size limit: 16 MB — never grow arrays without bound.
Duplicate small, rarely changing fields (customer name on an order) to avoid lookups.
Comparison:
$eq $ne $gt $gte $lt $lte $in $nin; logic: $and $or $not; arrays: $elemMatch, $all, $size.Update operators:
$set $unset $inc $push $pull $addToSet; upsert: true inserts if missing.Single-document updates are atomic; multi-document changes need a transaction (replica set).
Common mistakeNoSQL injection: passing
req.body straight into a query lets { "$gt": "" } match everything.FlowEach stage transforms the documents and passes them on$match→$lookup→$unwind→$group→$sort→$project→$limit
Every collection has an
_id index; add indexes for your query filters and sorts.Compound index field order: Equality → Sort → Range (the ESR rule).
Special indexes: unique, TTL (auto-expire sessions), text, geospatial, partial.
Check with
.explain("executionStats"): you want IXSCAN, not COLLSCAN.ReviseModel documents around queries: embed what is read together and bounded, reference what is shared or unbounded.