Skip to content

Relationships Example

This example shows the three relationship patterns supported by the library in one FastAPI app:

  • CRUDPopulate for referenced documents in another collection
  • CRUDEmbed for nested data inside the parent document
  • CRUDLookup for nested child routes
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
from typing import Optional, Union

from fastapi import FastAPI
import motor.motor_asyncio
from pydantic import Field
from fastapi_crudrouter_mongodb import (
    CRUDEmbed,
    CRUDLookup,
    CRUDPopulate,
    CRUDRouter,
    MongoModel,
    ObjectIdType,
)


client = motor.motor_asyncio.AsyncIOMotorClient("mongodb://localhost:27017/local")
db = client.local


class ArtistModel(MongoModel):
    id: ObjectIdType | None = None
    name: str


class ArtistModelOut(MongoModel):
    id: str
    name: str


class GenreModel(MongoModel):
    id: ObjectIdType | None = None
    label: str


class ReviewModel(MongoModel):
    id: ObjectIdType | None = None
    track_id: ObjectIdType
    rating: int
    comment: str


class TrackModel(MongoModel):
    id: ObjectIdType | None = None
    title: str
    artist_ids: list[ObjectIdType] = Field(default_factory=list)
    genres: list[GenreModel] = Field(default_factory=list)
    reviews: Optional[Union[list[ReviewModel], ReviewModel]] = None


class TrackModelOut(MongoModel):
    id: str
    title: str
    artist_ids: list[ArtistModelOut] = Field(default_factory=list)
    genres: list[GenreModel] = Field(default_factory=list)


artist_populate = CRUDPopulate(
    field="artist_ids",
    collection="artists",
    model=ArtistModel,
)

genres_embed = CRUDEmbed(
    model=GenreModel,
    embed_name="genres",
)

reviews_lookup = CRUDLookup(
    model=ReviewModel,
    collection_name="reviews",
    prefix="reviews",
    local_field="_id",
    foreign_field="track_id",
)


tracks_router = CRUDRouter(
    model=TrackModel,
    model_out=TrackModelOut,
    db=db,
    collection_name="tracks",
    prefix="/tracks",
    populates=[artist_populate],
    embeds=[genres_embed],
    lookups=[reviews_lookup],
    tags=["tracks"],
)

artists_router = CRUDRouter(
    model=ArtistModel,
    db=db,
    collection_name="artists",
    prefix="/artists",
    tags=["artists"],
)


app = FastAPI(title="Relationships Example")
app.include_router(artists_router)
app.include_router(tracks_router)

What this gives you

  • GET /tracks returns tracks and can populate artist_ids
  • POST /tracks/{id}/reviews creates a review linked to a track
  • GET /tracks/{id}/reviews lists reviews for a track
  • Embedded genres are stored inside each track document

This is a good starting point for music, catalog, and content APIs.