Introduction
Denormalization means storing related data together to make reading faster, but it can make updating data harder.
No specific syntax because denormalization is a design choice.
It means embedding related data inside documents instead of linking separate collections./* Normalized */ { _id: 1, name: "Alice", address_id: 101 } { _id: 101, street: "123 Main St", city: "Townsville" }
/* Denormalized */ { _id: 1, name: "Alice", address: { street: "123 Main St", city: "Townsville" } }
db.users.insertOne({
_id: 1,
name: "Alice",
address: {
street: "123 Main St",
city: "Townsville"
}
});
const user = db.users.findOne({_id: 1});
printjson(user);{ _id: 1, name: 'Alice', orders: [{id: 101, total: 50}] } embeds orders array inside user document, showing denormalization.{ _id: 1, name: 'Bob', address: { city: 'NY', zip: '10001' } }Pass the test and this pattern moves unknown