You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Unlike relational databases where schema design means normalising tables, MongoDB schema design is about choosing how to structure documents to best match your application's access patterns. The two main strategies are embedding and referencing.
Embedded documents store related data together in a single document. This is the preferred approach when the related data is always accessed together.
{
_id: ObjectId("..."),
name: "Alice",
address: {
street: "10 Downing St",
city: "London",
postcode: "SW1A 2AA"
},
phones: [
{ type: "mobile", number: "+44 7700 900001" },
{ type: "work", number: "+44 20 7946 0000" }
]
}
Advantages: Single-document reads are fast (no joins). Atomic updates on a single document are simple. Great for one-to-few relationships.
Disadvantages: Documents can grow large. If the embedded data is shared across many parent documents, you end up duplicating it.
References store the _id of a related document, similar to a foreign key. Use referencing for one-to-many or many-to-many relationships where the related data is large or shared.
// Article document
{ _id: ObjectId("a1"), title: "MongoDB Intro", authorId: ObjectId("u1") }
// Author document
{ _id: ObjectId("u1"), name: "Jane", bio: "Database engineer..." }
You then use $lookup in the aggregation pipeline to join the documents at query time.
Ask yourself: "Do I always access this data together?"
For a blog with comments, if comments are numerous and sometimes queried independently, store them in a separate collection and reference the post:
// posts collection
{ _id: ObjectId("p1"), title: "Hello World" }
// comments collection
{ _id: ObjectId("c1"), postId: ObjectId("p1"), text: "Great post!", author: "Bob" }
The bucket pattern groups a stream of data (e.g. sensor readings) into fixed-size documents to avoid unbounded array growth:
{
sensorId: "sensor-42",
hour: ISODate("2024-06-01T10:00:00Z"),
readings: [
{ ts: ISODate("2024-06-01T10:00:05Z"), temp: 21.3 },
{ ts: ISODate("2024-06-01T10:00:10Z"), temp: 21.5 }
],
count: 2
}
As your application evolves, add a schemaVersion field to documents so your application code can handle multiple document shapes gracefully:
{ _id: ObjectId("..."), schemaVersion: 2, name: "Alice", ... }
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.