beanie icon indicating copy to clipboard operation
beanie copied to clipboard

[BUG] Projection model not working in aggregation

Open valentinoli opened this issue 5 months ago • 0 comments

Describe the bug

When I provide a projection_model to FindMany.aggregate(), that has the id property with an alias="_id" it fails to project with the following error

_id
  Field required [type=missing, input_value={[**REDACTED**]}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.8/v/missing

This happens even if I set populate_by_name=True on the Pydantic model.

To Reproduce

from beanie import DocumentModel
from pydantic import BaseModel

class Doc(DocumentModel):
    id: str = Field(
        ...,
        alias="_id",
    )

class Model(BaseModel):
    model_config = ConfigDict(
        populate_by_name=True,
    )

    id: str = Field(
        ...,
        alias="_id",
    )

async def query():
    results = await Doc.find().aggregate(aggregation_pipeline=[], projection_model=Model).to_list()
    return results

Expected behavior This should work. I should not have to manually project each result from the query, like [Model(**res) for res in results]

valentinoli avatar Sep 05 '24 12:09 valentinoli