Closed nemrok closed 1 year ago
Projection model is as such:
class GenreCount(BaseModel):
genre: Optional[str] = 'N/A'
count: int
class YearCount(BaseModel):
year: int
genres: List[GenreCount]
count: int
class ArtistCount(BaseModel):
artist: Optional[str] = None
years: Optional[List[YearCount]] = []
count: Optional[int] = 0
I solved the issue. It turns out that Song.artist is a DbRef and not an ObjectId. So I updated the pipeline as such and now it's working. I will close the issue. Thanks regardless!
{
'$lookup': {
'from': Artist.get_collection_name(),
'localField': 'artist.$id',
'foreignField': '_id',
'as': 'artist'
}
},
{
'$unwind': '$artist'
}
...
}
Describe the bug First off, I love this framework. I'm looking to move from MongoEngine to Beanie since I use FastApi, AsyncIO and Pydantic for all my projects. I use a lot of aggregations and ran into an issue with $lookup in the pipeline within an aggregate call. It fails to find the linked document. Thanks in advance for looking into this!
To Reproduce
Expected behavior The $lookup call fails to find the 'Artist' document which is linked to the 'Song' document. Since the Artist document is never found, the aggregate call returns no results. As you can see the pipeline syntax is standard and should work. I have used aggregations like this in Pymongo without issue. Is this a limitation with the current Beanie implementation?
Additional context Here is how the Song and Artist collections are defined: