| 1 |
from abc import abstractmethod |
| 2 |
from typing import Any, cast |
| 3 |
|
| 4 |
from pymongo.asynchronous.client_session import AsyncClientSession |
| 5 |
|
| 6 |
from beanie.odm.fields import ExpressionField |
| 7 |
|
| 8 |
|
| 9 |
class AggregateMethods: |
| 10 |
""" |
| 11 |
Aggregate methods |
| 12 |
""" |
| 13 |
|
| 14 |
@abstractmethod |
| 15 |
def aggregate( |
| 16 |
self, |
| 17 |
aggregation_pipeline, |
| 18 |
projection_model=None, |
| 19 |
session: AsyncClientSession | None = None, |
| 20 |
ignore_cache: bool = False, |
| 21 |
): ... |
| 22 |
|
| 23 |
async def sum( |
| 24 |
self, |
| 25 |
field: ExpressionField | float | int | str, |
| 26 |
session: AsyncClientSession | None = None, |
| 27 |
ignore_cache: bool = False, |
| 28 |
) -> float | None: |
| 29 |
""" |
| 30 |
Sum of values of the given field |
| 31 |
|
| 32 |
Example: |
| 33 |
|
| 34 |
```python |
| 35 |
|
| 36 |
class Sample(Document): |
| 37 |
price: int |
| 38 |
count: int |
| 39 |
|
| 40 |
sum_count = await Document.find(Sample.price <= 100).sum(Sample.count) |
| 41 |
|
| 42 |
``` |
| 43 |
|
| 44 |
:param field: Union[ExpressionField, float, int, str] |
| 45 |
:param session: Optional[AsyncClientSession] - pymongo session |
| 46 |
:param ignore_cache: bool |
| 47 |
:return: float - sum. None if there are no items. |
| 48 |
""" |
| 49 |
pipeline = [ |
| 50 |
{"$group": {"_id": None, "sum": {"$sum": f"${field}"}}}, |
| 51 |
{"$project": {"_id": 0, "sum": 1}}, |
| 52 |
] |
| 53 |
|
| 54 |
# As we did not supply a projection we can safely cast the type (hinting to mypy that we know the type) |
| 55 |
result: list[dict[str, Any]] = cast( |
| 56 |
list[dict[str, Any]], |
| 57 |
await self.aggregate( |
| 58 |
aggregation_pipeline=pipeline, |
| 59 |
session=session, |
| 60 |
ignore_cache=ignore_cache, |
| 61 |
).to_list(), # type: ignore # TODO: pyright issue, fix |
| 62 |
) |
| 63 |
if not result: |
| 64 |
return None |
| 65 |
return result[0]["sum"] |
| 66 |
|
| 67 |
async def avg( |
| 68 |
self, |
| 69 |
field: ExpressionField | float | int | str, |
| 70 |
session: AsyncClientSession | None = None, |
| 71 |
ignore_cache: bool = False, |
| 72 |
) -> float | None: |
| 73 |
""" |
| 74 |
Average of values of the given field |
| 75 |
|
| 76 |
Example: |
| 77 |
|
| 78 |
```python |
| 79 |
|
| 80 |
class Sample(Document): |
| 81 |
price: int |
| 82 |
count: int |
| 83 |
|
| 84 |
avg_count = await Document.find(Sample.price <= 100).avg(Sample.count) |
| 85 |
``` |
| 86 |
|
| 87 |
:param field: Union[ExpressionField, float, int, str] |
| 88 |
:param session: Optional[AsyncClientSession] - pymongo session |
| 89 |
:param ignore_cache: bool |
| 90 |
:return: Optional[float] - avg. None if there are no items. |
| 91 |
""" |
| 92 |
pipeline = [ |
| 93 |
{"$group": {"_id": None, "avg": {"$avg": f"${field}"}}}, |
| 94 |
{"$project": {"_id": 0, "avg": 1}}, |
| 95 |
] |
| 96 |
|
| 97 |
result: list[dict[str, Any]] = cast( |
| 98 |
list[dict[str, Any]], |
| 99 |
await self.aggregate( |
| 100 |
aggregation_pipeline=pipeline, |
| 101 |
session=session, |
| 102 |
ignore_cache=ignore_cache, |
| 103 |
).to_list(), # type: ignore # TODO: pyright issue, fix |
| 104 |
) |
| 105 |
if not result: |
| 106 |
return None |
| 107 |
return result[0]["avg"] |
| 108 |
|
| 109 |
async def max( |
| 110 |
self, |
| 111 |
field: ExpressionField | str | Any, |
| 112 |
session: AsyncClientSession | None = None, |
| 113 |
ignore_cache: bool = False, |
| 114 |
) -> Any | None: |
| 115 |
""" |
| 116 |
Max of the values of the given field |
| 117 |
|
| 118 |
Example: |
| 119 |
|
| 120 |
```python |
| 121 |
|
| 122 |
class Sample(Document): |
| 123 |
price: int |
| 124 |
count: int |
| 125 |
|
| 126 |
max_count = await Document.find(Sample.price <= 100).max(Sample.count) |
| 127 |
``` |
| 128 |
|
| 129 |
:param field: Union[ExpressionField, str, Any] |
| 130 |
:param session: Optional[AsyncClientSession] - pymongo session |
| 131 |
:return: Any - max value. None if there are no items. |
| 132 |
""" |
| 133 |
pipeline = [ |
| 134 |
{"$group": {"_id": None, "max": {"$max": f"${field}"}}}, |
| 135 |
{"$project": {"_id": 0, "max": 1}}, |
| 136 |
] |
| 137 |
|
| 138 |
result: list[dict[str, Any]] = cast( |
| 139 |
list[dict[str, Any]], |
| 140 |
await self.aggregate( |
| 141 |
aggregation_pipeline=pipeline, |
| 142 |
session=session, |
| 143 |
ignore_cache=ignore_cache, |
| 144 |
).to_list(), # type: ignore # TODO: pyright issue, fix |
| 145 |
) |
| 146 |
if not result: |
| 147 |
return None |
| 148 |
return result[0]["max"] |
| 149 |
|
| 150 |
async def min( |
| 151 |
self, |
| 152 |
field: ExpressionField | str | Any, |
| 153 |
session: AsyncClientSession | None = None, |
| 154 |
ignore_cache: bool = False, |
| 155 |
) -> Any | None: |
| 156 |
""" |
| 157 |
Min of the values of the given field |
| 158 |
|
| 159 |
Example: |
| 160 |
|
| 161 |
```python |
| 162 |
|
| 163 |
class Sample(Document): |
| 164 |
price: int |
| 165 |
count: int |
| 166 |
|
| 167 |
min_count = await Document.find(Sample.price <= 100).min(Sample.count) |
| 168 |
``` |
| 169 |
|
| 170 |
:param field: Union[ExpressionField, str, Any] |
| 171 |
:param session: Optional[AsyncClientSession] - pymongo session |
| 172 |
:return: Any - min value. None if there are no items. |
| 173 |
""" |
| 174 |
pipeline = [ |
| 175 |
{"$group": {"_id": None, "min": {"$min": f"${field}"}}}, |
| 176 |
{"$project": {"_id": 0, "min": 1}}, |
| 177 |
] |
| 178 |
|
| 179 |
result: list[dict[str, Any]] = cast( |
| 180 |
list[dict[str, Any]], |
| 181 |
await self.aggregate( |
| 182 |
aggregation_pipeline=pipeline, |
| 183 |
session=session, |
| 184 |
ignore_cache=ignore_cache, |
| 185 |
).to_list(), # type: ignore # TODO: pyright issue, fix |
| 186 |
) |
| 187 |
if not result: |
| 188 |
return None |
| 189 |
return result[0]["min"] |