18. Multiple Collections and Relationships in MongoDB

Designing Multiple Collections in MongoDB

MongoDB favors modeling data around access patterns rather than strict normalization. Multiple collections are used to represent distinct domain entities while keeping documents focused and efficient. Proper collection design reduces query complexity and supports scalable data growth.


Managing Relationships Without Joins

MongoDB does not rely on joins as a primary relationship mechanism. Instead, relationships are modeled using references or embedded documents depending on read and write patterns. Choosing the appropriate strategy directly impacts performance, consistency, and query simplicity.

// tourists collection
{
  "_id": "tourist_id",
  "name": "Alice",
  "country": "Germany"
}

// tourist_transactions collection
{
  "_id": "txn_id",
  "tourist_id": "tourist_id",
  "amount": 1200,
  "currency": "EUR"
}

FastAPI routes should reflect domain relationships without exposing internal database implementation details. Related data is typically accessed through coordinated queries or aggregation pipelines. Clear route structure improves API readability and client usability.


MongoDB aggregation pipelines allow related data to be processed and summarized efficiently. They enable filtering, grouping, and transformation without loading large datasets into application memory. Aggregations are essential for relationship-based queries in analytics-driven applications.


Testing Relationship-Based Endpoints Using cURL

cURL commands provide a simple way to validate relationship-based routes. They help verify that related data is retrieved correctly and that route design remains intuitive for API consumers.

Fetch Transactions for a Tourist


Operational Perspective

Managing relationships across collections requires deliberate design and disciplined access patterns. By combining clear data modeling, well-structured routes, and aggregation pipelines, FastAPI and MongoDB can support complex relational use cases without traditional joins.

Last updated