Debian Codegraph @

beanie/odm/interfaces/aggregate.py

raw
1 from abc import abstractmethod
2 from typing import Any, TypeVar, overload
3
4 from pydantic import BaseModel
5 from pymongo.asynchronous.client_session import AsyncClientSession
6
7 from beanie.odm.fields import ExpressionField
8 from beanie.odm.queries.aggregation import AggregationQuery
9 from beanie.odm.queries.find import FindMany
10
11 DocType = TypeVar("DocType", bound="AggregateInterface")
12 DocumentProjectionType = TypeVar("DocumentProjectionType", bound=BaseModel)
13
14
15 class AggregateInterface:
16 @classmethod
17 @abstractmethod
18 def find_all(cls) -> FindMany:
19 pass
20
21 @overload
22 @classmethod
23 def aggregate(
24 cls: type[DocType],
25 aggregation_pipeline: list,
26 projection_model: None = None,
27 session: AsyncClientSession | None = None,
28 ignore_cache: bool = False,
29 **pymongo_kwargs: Any,
30 ) -> AggregationQuery[dict[str, Any]]: ...
31
32 @overload
33 @classmethod
34 def aggregate(
35 cls: type[DocType],
36 aggregation_pipeline: list,
37 projection_model: type[DocumentProjectionType],
38 session: AsyncClientSession | None = None,
39 ignore_cache: bool = False,
40 **pymongo_kwargs: Any,
41 ) -> AggregationQuery[DocumentProjectionType]: ...
42
43 @classmethod
44 def aggregate(
45 cls: type[DocType],
46 aggregation_pipeline: list,
47 projection_model: type[DocumentProjectionType] | None = None,
48 session: AsyncClientSession | None = None,
49 ignore_cache: bool = False,
50 **pymongo_kwargs: Any,
51 ) -> (
52 AggregationQuery[dict[str, Any]]
53 | AggregationQuery[DocumentProjectionType]
54 ):
55 """
56 Aggregate over collection.
57 Returns [AggregationQuery](query.md#aggregationquery) query object
58 :param aggregation_pipeline: list - aggregation pipeline
59 :param projection_model: Type[BaseModel]
60 :param session: Optional[AsyncClientSession] - pymongo session.
61 :param ignore_cache: bool
62 :param **pymongo_kwargs: pymongo native parameters for aggregate operation
63 :return: [AggregationQuery](query.md#aggregationquery)
64 """
65 return cls.find_all().aggregate(
66 aggregation_pipeline=aggregation_pipeline,
67 projection_model=projection_model,
68 session=session,
69 ignore_cache=ignore_cache,
70 **pymongo_kwargs,
71 )
72
73 @classmethod
74 async def sum(
75 cls,
76 field: ExpressionField | float | int | str,
77 session: AsyncClientSession | None = None,
78 ignore_cache: bool = False,
79 ) -> float | None:
80 """
81 Sum of values of the given field over the entire collection.
82
83 Example:
84
85 ```python
86
87 class Sample(Document):
88 price: int
89
90 sum_count = await Document.sum(Sample.price)
91
92 ```
93
94 :param field: Union[ExpressionField, float, int, str]
95 :param session: Optional[AsyncClientSession] - pymongo session
96 :param ignore_cache: bool
97 :return: float - sum. None if there are no items.
98 """
99 return await cls.find_all().sum(field, session, ignore_cache)
100
101 @classmethod
102 async def avg(
103 cls,
104 field: ExpressionField | float | int | str,
105 session: AsyncClientSession | None = None,
106 ignore_cache: bool = False,
107 ) -> float | None:
108 """
109 Average of values of the given field over the entire collection.
110
111 Example:
112
113 ```python
114
115 class Sample(Document):
116 price: int
117
118 avg_count = await Document.avg(Sample.price)
119 ```
120
121 :param field: Union[ExpressionField, float, int, str]
122 :param session: Optional[AsyncClientSession] - pymongo session
123 :param ignore_cache: bool
124 :return: Optional[float] - avg. None if there are no items.
125 """
126 return await cls.find_all().avg(field, session, ignore_cache)
127
128 @classmethod
129 async def max(
130 cls,
131 field: ExpressionField | str | Any,
132 session: AsyncClientSession | None = None,
133 ignore_cache: bool = False,
134 ) -> Any | None:
135 """
136 Max of the values of the given field over the entire collection.
137
138 Example:
139
140 ```python
141
142 class Sample(Document):
143 price: int
144
145 max_count = await Document.max(Sample.price)
146 ```
147
148 :param field: Union[ExpressionField, str, Any]
149 :param session: Optional[AsyncClientSession] - pymongo session
150 :return: Any - max value. None if there are no items.
151 """
152 return await cls.find_all().max(field, session, ignore_cache)
153
154 @classmethod
155 async def min(
156 cls,
157 field: ExpressionField | str | Any,
158 session: AsyncClientSession | None = None,
159 ignore_cache: bool = False,
160 ) -> Any | None:
161 """
162 Min of the values of the given field over the entire collection.
163
164 Example:
165
166 ```python
167
168 class Sample(Document):
169 price: int
170
171 min_count = await Document.min(Sample.price)
172 ```
173
174 :param field: Union[ExpressionField, str, Any]
175 :param session: Optional[AsyncClientSession] - pymongo session
176 :return: Any - min value. None if there are no items.
177 """
178 return await cls.find_all().min(field, session, ignore_cache)