Introduction
Good document design helps your database work faster and makes it easier to find and use your data.
Jump into concepts and practice - no test required
Good document design helps your database work faster and makes it easier to find and use your data.
No fixed syntax because document design is about how you organize data inside documents in MongoDB collections.
{
"name": "Alice",
"age": 30,
"orders": [
{"order_id": 1, "item": "Book"},
{"order_id": 2, "item": "Pen"}
]
}{
"product_id": 101,
"name": "Notebook",
"price": 5.99
}This example adds a customer with their orders in one document, then retrieves it all at once.
db.customers.insertOne({
name: "Bob",
age: 25,
orders: [
{ order_id: 101, item: "Laptop" },
{ order_id: 102, item: "Mouse" }
]
});
const customer = db.customers.findOne({ name: "Bob" });
printjson(customer);Embedding related data in one document can make reading faster.
But if data grows too big or changes often, splitting into multiple documents might be better.
Think about how your app uses data to decide the best design.
Good document design helps your database work efficiently.
It groups related data together to make access simple and fast.
Design depends on your app's needs and how data changes over time.
{ _id: 1, name: 'Bob', orders: [{ id: 101, total: 50 }, { id: 102, total: 30 }] }db.users.findOne({ _id: 1 })?{ title: 'Post', comments: 'Great post!' }