1 · MongoDB · Document · 2 of 2
← Lesson 2: 3NF
run
Referencing: bring to the surface
// item_configs
{ _id: "axe_dusk", effect_ids: ["burn", "freeze"] }
// item_effects
{ _id: "burn", title: "Горение", damage_per_sec: 6 }
{ $lookup: { from: "item_effects", localField: "effect_ids",
foreignField: "_id", as: "effects" } }
We update the shared fact in one place.
New requests to the link see version 6. The cache that has already been loaded won’t update on its own.
We pay for this with data collection.
$lookup combines them on the server into a single query
REMOVE IF
REMOVE IF
REMOVE IF
It grows without limit
He lives his own life
A copy is hard to manage
There can be an unlimited number of children: each child has a `parent_id` and an index based on that ID, rather than an infinite array of IDs.
A child entity exists independently of its parent; it is not the only one that references it, and it is archived separately.
The value changes, and the user will notice the discrepancy. We need a "source of truth," an acceptable lag, and a reconciliation process.
There are no external keys. effect_ids: ["no_such_effect"] $lookup will return []. If damage depends solely on the effect, having a copy in every item causes update anomalies—this is analogous to 3NF.
▶ P9: Indices