MongoDB — Relationships
Relationship Types
| Type | Description |
|---|---|
| One-to-One | Single related document |
| One-to-Many | Multiple related documents |
| Many-to-Many | Multiple on both sides |
Embedding (One-to-One)
{
name: "John",
profile: {
bio: "Developer",
website: "example.com"
}
}
Referencing (One-to-Many)
// User document
{ _id: ObjectId("..."), name: "John" }
// Order documents
{ userId: ObjectId("..."), total: 100 }
Population
db.orders.aggregate([
{
$lookup: {
from: "users",
localField: "userId",
foreignField: "_id",
as: "user"
}
}
])
Mini Practice
- Design one-to-one
- Design one-to-many
- Use $lookup for references
- Choose embedding vs referencing
Up Next
Continue with Transactions — multi-document transactions.
Related Topics
Frequently Asked Questions about Relationships
What is Relationships in MongoDB?
Relationships is a fundamental concept in MongoDB. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Relationships?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Relationships.
Why is Relationships important in MongoDB?
Relationships is essential for MongoDB development. Understanding this concept will help you write better code and solve real-world problems more effectively.