BeanieODM / beanie

Asynchronous Python ODM for MongoDB
http://beanie-odm.dev/
Apache License 2.0
2k stars 211 forks source link

[BUG] The aggregation operations after using `fetch_links=True` are all invalid #496

Closed Evisolpxe closed 1 year ago

Evisolpxe commented 1 year ago

To Reproduce

class Match(Document):
    match_id: Indexed(int, unique=True)
    score: int = 50

class Record(Document):
    name: Indexed(str)
    match: Link[Match]

print(await Record.find("match.match_id" : 1}, fetch_links=True).to_list())
# [Record(id=ObjectId('63f37ee1c07af0c8699b43fc'), ...]  it's ok
print(await Record.find("match.match_id" : 1}, fetch_links=True).delete()
# <pymongo.results.DeleteResult object at 0x7f3ab5046050>  nothing happened
print(await Record.find("match.match_id" : 1}, fetch_links=True).count()
# 0
print(await Record.find("match.match_id" : 1}, fetch_links=True).aggregate([...])
# same invalid

Expected behavior working well

sabattle commented 1 year ago

Hey, just confirming that I'm also experiencing this. Using fetch_links essentially is breaking every aggregation operation.

roman-right commented 1 year ago

Hi! Sorry for the delay. I'll pick this up soon

MrEarle commented 1 year ago

Hi! I know this is in your current TODO, but is there an expected timeframe where this would be fixed? 😅 I'm currently working around this by manually building the aggregations for counting with fetch_links=True.

roman-right commented 1 year ago

I've made a fix for this. It will be merged together with other fixes by next Monday.

.count() is already supported .delete() and .update() will not be supported as it is impossible to mix delete/update with aggregation while find with fetch_links=True is an aggregation under the hood. I'll mention this in the doc .aggregate() will be supported after the fix will be merged.

PR: https://github.com/roman-right/beanie/pull/571