mongo-aggro 0.1.0__py3-none-any.whl → 0.2.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mongo_aggro/__init__.py +400 -0
- mongo_aggro/accumulators.py +30 -12
- mongo_aggro/base.py +49 -9
- mongo_aggro/expressions/__init__.py +396 -0
- mongo_aggro/expressions/arithmetic.py +329 -0
- mongo_aggro/expressions/array.py +425 -0
- mongo_aggro/expressions/base.py +180 -0
- mongo_aggro/expressions/bitwise.py +84 -0
- mongo_aggro/expressions/comparison.py +161 -0
- mongo_aggro/expressions/conditional.py +117 -0
- mongo_aggro/expressions/date.py +665 -0
- mongo_aggro/expressions/encrypted.py +116 -0
- mongo_aggro/expressions/logical.py +72 -0
- mongo_aggro/expressions/object.py +122 -0
- mongo_aggro/expressions/set.py +150 -0
- mongo_aggro/expressions/size.py +48 -0
- mongo_aggro/expressions/string.py +365 -0
- mongo_aggro/expressions/trigonometry.py +283 -0
- mongo_aggro/expressions/type.py +205 -0
- mongo_aggro/expressions/variable.py +73 -0
- mongo_aggro/expressions/window.py +327 -0
- mongo_aggro/operators/__init__.py +65 -0
- mongo_aggro/operators/array.py +41 -0
- mongo_aggro/operators/base.py +15 -0
- mongo_aggro/operators/bitwise.py +81 -0
- mongo_aggro/operators/comparison.py +82 -0
- mongo_aggro/operators/element.py +32 -0
- mongo_aggro/operators/geo.py +171 -0
- mongo_aggro/operators/logical.py +111 -0
- mongo_aggro/operators/misc.py +102 -0
- mongo_aggro/operators/regex.py +25 -0
- mongo_aggro/stages/__init__.py +110 -0
- mongo_aggro/stages/array.py +69 -0
- mongo_aggro/stages/change.py +109 -0
- mongo_aggro/stages/core.py +170 -0
- mongo_aggro/stages/geo.py +93 -0
- mongo_aggro/stages/group.py +154 -0
- mongo_aggro/stages/join.py +221 -0
- mongo_aggro/stages/misc.py +45 -0
- mongo_aggro/stages/output.py +136 -0
- mongo_aggro/stages/search.py +315 -0
- mongo_aggro/stages/session.py +111 -0
- mongo_aggro/stages/stats.py +152 -0
- mongo_aggro/stages/transform.py +136 -0
- mongo_aggro/stages/window.py +139 -0
- mongo_aggro-0.2.2.dist-info/METADATA +193 -0
- mongo_aggro-0.2.2.dist-info/RECORD +49 -0
- {mongo_aggro-0.1.0.dist-info → mongo_aggro-0.2.2.dist-info}/WHEEL +1 -1
- mongo_aggro/operators.py +0 -247
- mongo_aggro/stages.py +0 -990
- mongo_aggro-0.1.0.dist-info/METADATA +0 -537
- mongo_aggro-0.1.0.dist-info/RECORD +0 -9
- {mongo_aggro-0.1.0.dist-info → mongo_aggro-0.2.2.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,665 @@
|
|
|
1
|
+
"""Date expression operators for MongoDB aggregation."""
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from pydantic import model_serializer
|
|
6
|
+
|
|
7
|
+
from mongo_aggro.base import serialize_value
|
|
8
|
+
from mongo_aggro.expressions.base import ExpressionBase
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class DateAddExpr(ExpressionBase):
|
|
12
|
+
"""
|
|
13
|
+
$dateAdd expression operator - adds time to a date.
|
|
14
|
+
|
|
15
|
+
Example:
|
|
16
|
+
>>> DateAddExpr(
|
|
17
|
+
... start_date=F("orderDate"),
|
|
18
|
+
... unit="day",
|
|
19
|
+
... amount=7
|
|
20
|
+
... ).model_dump()
|
|
21
|
+
{"$dateAdd": {"startDate": "$orderDate", "unit": "day", "amount": 7}}
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
start_date: Any
|
|
25
|
+
unit: str
|
|
26
|
+
amount: Any
|
|
27
|
+
timezone: str | None = None
|
|
28
|
+
|
|
29
|
+
@model_serializer
|
|
30
|
+
def serialize(self) -> dict[str, Any]:
|
|
31
|
+
"""Serialize to MongoDB $dateAdd expression."""
|
|
32
|
+
result: dict[str, Any] = {
|
|
33
|
+
"$dateAdd": {
|
|
34
|
+
"startDate": serialize_value(self.start_date),
|
|
35
|
+
"unit": self.unit,
|
|
36
|
+
"amount": serialize_value(self.amount),
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if self.timezone:
|
|
40
|
+
result["$dateAdd"]["timezone"] = self.timezone
|
|
41
|
+
return result
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class DateSubtractExpr(ExpressionBase):
|
|
45
|
+
"""
|
|
46
|
+
$dateSubtract expression operator - subtracts time from a date.
|
|
47
|
+
|
|
48
|
+
Example:
|
|
49
|
+
>>> DateSubtractExpr(
|
|
50
|
+
... start_date=F("endDate"),
|
|
51
|
+
... unit="month",
|
|
52
|
+
... amount=1
|
|
53
|
+
... ).model_dump()
|
|
54
|
+
{"$dateSubtract": {"startDate": "$endDate", "unit": "month", "amount": 1}}
|
|
55
|
+
"""
|
|
56
|
+
|
|
57
|
+
start_date: Any
|
|
58
|
+
unit: str
|
|
59
|
+
amount: Any
|
|
60
|
+
timezone: str | None = None
|
|
61
|
+
|
|
62
|
+
@model_serializer
|
|
63
|
+
def serialize(self) -> dict[str, Any]:
|
|
64
|
+
"""Serialize to MongoDB $dateSubtract expression."""
|
|
65
|
+
result: dict[str, Any] = {
|
|
66
|
+
"$dateSubtract": {
|
|
67
|
+
"startDate": serialize_value(self.start_date),
|
|
68
|
+
"unit": self.unit,
|
|
69
|
+
"amount": serialize_value(self.amount),
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if self.timezone:
|
|
73
|
+
result["$dateSubtract"]["timezone"] = self.timezone
|
|
74
|
+
return result
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class DateDiffExpr(ExpressionBase):
|
|
78
|
+
"""
|
|
79
|
+
$dateDiff expression operator - calculates difference between dates.
|
|
80
|
+
|
|
81
|
+
Example:
|
|
82
|
+
>>> DateDiffExpr(
|
|
83
|
+
... start_date=F("start"),
|
|
84
|
+
... end_date=F("end"),
|
|
85
|
+
... unit="day"
|
|
86
|
+
... ).model_dump()
|
|
87
|
+
{"$dateDiff": {"startDate": "$start", "endDate": "$end", "unit": "day"}}
|
|
88
|
+
"""
|
|
89
|
+
|
|
90
|
+
start_date: Any
|
|
91
|
+
end_date: Any
|
|
92
|
+
unit: str
|
|
93
|
+
timezone: str | None = None
|
|
94
|
+
start_of_week: str | None = None
|
|
95
|
+
|
|
96
|
+
@model_serializer
|
|
97
|
+
def serialize(self) -> dict[str, Any]:
|
|
98
|
+
"""Serialize to MongoDB $dateDiff expression."""
|
|
99
|
+
result: dict[str, Any] = {
|
|
100
|
+
"$dateDiff": {
|
|
101
|
+
"startDate": serialize_value(self.start_date),
|
|
102
|
+
"endDate": serialize_value(self.end_date),
|
|
103
|
+
"unit": self.unit,
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if self.timezone:
|
|
107
|
+
result["$dateDiff"]["timezone"] = self.timezone
|
|
108
|
+
if self.start_of_week:
|
|
109
|
+
result["$dateDiff"]["startOfWeek"] = self.start_of_week
|
|
110
|
+
return result
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
class DateToStringExpr(ExpressionBase):
|
|
114
|
+
"""
|
|
115
|
+
$dateToString expression operator - converts date to string.
|
|
116
|
+
|
|
117
|
+
Example:
|
|
118
|
+
>>> DateToStringExpr(
|
|
119
|
+
... date=F("orderDate"),
|
|
120
|
+
... format="%Y-%m-%d"
|
|
121
|
+
... ).model_dump()
|
|
122
|
+
{"$dateToString": {"date": "$orderDate", "format": "%Y-%m-%d"}}
|
|
123
|
+
"""
|
|
124
|
+
|
|
125
|
+
date: Any
|
|
126
|
+
format: str | None = None
|
|
127
|
+
timezone: str | None = None
|
|
128
|
+
on_null: Any = None
|
|
129
|
+
|
|
130
|
+
@model_serializer
|
|
131
|
+
def serialize(self) -> dict[str, Any]:
|
|
132
|
+
"""Serialize to MongoDB $dateToString expression."""
|
|
133
|
+
result: dict[str, Any] = {
|
|
134
|
+
"$dateToString": {
|
|
135
|
+
"date": serialize_value(self.date),
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if self.format:
|
|
139
|
+
result["$dateToString"]["format"] = self.format
|
|
140
|
+
if self.timezone:
|
|
141
|
+
result["$dateToString"]["timezone"] = self.timezone
|
|
142
|
+
if self.on_null is not None:
|
|
143
|
+
result["$dateToString"]["onNull"] = serialize_value(self.on_null)
|
|
144
|
+
return result
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
class DateFromStringExpr(ExpressionBase):
|
|
148
|
+
"""
|
|
149
|
+
$dateFromString expression operator - parses string to date.
|
|
150
|
+
|
|
151
|
+
Example:
|
|
152
|
+
>>> DateFromStringExpr(
|
|
153
|
+
... date_string=F("dateStr"),
|
|
154
|
+
... format="%Y-%m-%d"
|
|
155
|
+
... ).model_dump()
|
|
156
|
+
{"$dateFromString": {"dateString": "$dateStr", "format": "%Y-%m-%d"}}
|
|
157
|
+
"""
|
|
158
|
+
|
|
159
|
+
date_string: Any
|
|
160
|
+
format: str | None = None
|
|
161
|
+
timezone: str | None = None
|
|
162
|
+
on_error: Any = None
|
|
163
|
+
on_null: Any = None
|
|
164
|
+
|
|
165
|
+
@model_serializer
|
|
166
|
+
def serialize(self) -> dict[str, Any]:
|
|
167
|
+
"""Serialize to MongoDB $dateFromString expression."""
|
|
168
|
+
result: dict[str, Any] = {
|
|
169
|
+
"$dateFromString": {
|
|
170
|
+
"dateString": serialize_value(self.date_string),
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if self.format:
|
|
174
|
+
result["$dateFromString"]["format"] = self.format
|
|
175
|
+
if self.timezone:
|
|
176
|
+
result["$dateFromString"]["timezone"] = self.timezone
|
|
177
|
+
if self.on_error is not None:
|
|
178
|
+
result["$dateFromString"]["onError"] = serialize_value(
|
|
179
|
+
self.on_error
|
|
180
|
+
)
|
|
181
|
+
if self.on_null is not None:
|
|
182
|
+
result["$dateFromString"]["onNull"] = serialize_value(self.on_null)
|
|
183
|
+
return result
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
class ToDateExpr(ExpressionBase):
|
|
187
|
+
"""
|
|
188
|
+
$toDate expression operator - converts value to date.
|
|
189
|
+
|
|
190
|
+
Example:
|
|
191
|
+
>>> ToDateExpr(input=F("dateString")).model_dump()
|
|
192
|
+
{"$toDate": "$dateString"}
|
|
193
|
+
"""
|
|
194
|
+
|
|
195
|
+
input: Any
|
|
196
|
+
|
|
197
|
+
@model_serializer
|
|
198
|
+
def serialize(self) -> dict[str, Any]:
|
|
199
|
+
"""Serialize to MongoDB $toDate expression."""
|
|
200
|
+
return {"$toDate": serialize_value(self.input)}
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
class YearExpr(ExpressionBase):
|
|
204
|
+
"""
|
|
205
|
+
$year expression operator - extracts year from date.
|
|
206
|
+
|
|
207
|
+
Example:
|
|
208
|
+
>>> YearExpr(date=F("createdAt")).model_dump()
|
|
209
|
+
{"$year": "$createdAt"}
|
|
210
|
+
"""
|
|
211
|
+
|
|
212
|
+
date: Any
|
|
213
|
+
timezone: str | None = None
|
|
214
|
+
|
|
215
|
+
@model_serializer
|
|
216
|
+
def serialize(self) -> dict[str, Any]:
|
|
217
|
+
"""Serialize to MongoDB $year expression."""
|
|
218
|
+
if self.timezone is None:
|
|
219
|
+
return {"$year": serialize_value(self.date)}
|
|
220
|
+
return {
|
|
221
|
+
"$year": {
|
|
222
|
+
"date": serialize_value(self.date),
|
|
223
|
+
"timezone": self.timezone,
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
class MonthExpr(ExpressionBase):
|
|
229
|
+
"""
|
|
230
|
+
$month expression operator - extracts month (1-12) from date.
|
|
231
|
+
|
|
232
|
+
Example:
|
|
233
|
+
>>> MonthExpr(date=F("createdAt")).model_dump()
|
|
234
|
+
{"$month": "$createdAt"}
|
|
235
|
+
"""
|
|
236
|
+
|
|
237
|
+
date: Any
|
|
238
|
+
timezone: str | None = None
|
|
239
|
+
|
|
240
|
+
@model_serializer
|
|
241
|
+
def serialize(self) -> dict[str, Any]:
|
|
242
|
+
"""Serialize to MongoDB $month expression."""
|
|
243
|
+
if self.timezone is None:
|
|
244
|
+
return {"$month": serialize_value(self.date)}
|
|
245
|
+
return {
|
|
246
|
+
"$month": {
|
|
247
|
+
"date": serialize_value(self.date),
|
|
248
|
+
"timezone": self.timezone,
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
class DayOfMonthExpr(ExpressionBase):
|
|
254
|
+
"""
|
|
255
|
+
$dayOfMonth expression operator - extracts day of month (1-31).
|
|
256
|
+
|
|
257
|
+
Example:
|
|
258
|
+
>>> DayOfMonthExpr(date=F("createdAt")).model_dump()
|
|
259
|
+
{"$dayOfMonth": "$createdAt"}
|
|
260
|
+
"""
|
|
261
|
+
|
|
262
|
+
date: Any
|
|
263
|
+
timezone: str | None = None
|
|
264
|
+
|
|
265
|
+
@model_serializer
|
|
266
|
+
def serialize(self) -> dict[str, Any]:
|
|
267
|
+
"""Serialize to MongoDB $dayOfMonth expression."""
|
|
268
|
+
if self.timezone is None:
|
|
269
|
+
return {"$dayOfMonth": serialize_value(self.date)}
|
|
270
|
+
return {
|
|
271
|
+
"$dayOfMonth": {
|
|
272
|
+
"date": serialize_value(self.date),
|
|
273
|
+
"timezone": self.timezone,
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
class DayOfWeekExpr(ExpressionBase):
|
|
279
|
+
"""
|
|
280
|
+
$dayOfWeek expression operator - extracts day of week (1=Sun, 7=Sat).
|
|
281
|
+
|
|
282
|
+
Example:
|
|
283
|
+
>>> DayOfWeekExpr(date=F("createdAt")).model_dump()
|
|
284
|
+
{"$dayOfWeek": "$createdAt"}
|
|
285
|
+
"""
|
|
286
|
+
|
|
287
|
+
date: Any
|
|
288
|
+
timezone: str | None = None
|
|
289
|
+
|
|
290
|
+
@model_serializer
|
|
291
|
+
def serialize(self) -> dict[str, Any]:
|
|
292
|
+
"""Serialize to MongoDB $dayOfWeek expression."""
|
|
293
|
+
if self.timezone is None:
|
|
294
|
+
return {"$dayOfWeek": serialize_value(self.date)}
|
|
295
|
+
return {
|
|
296
|
+
"$dayOfWeek": {
|
|
297
|
+
"date": serialize_value(self.date),
|
|
298
|
+
"timezone": self.timezone,
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
class DayOfYearExpr(ExpressionBase):
|
|
304
|
+
"""
|
|
305
|
+
$dayOfYear expression operator - extracts day of year (1-366).
|
|
306
|
+
|
|
307
|
+
Example:
|
|
308
|
+
>>> DayOfYearExpr(date=F("createdAt")).model_dump()
|
|
309
|
+
{"$dayOfYear": "$createdAt"}
|
|
310
|
+
"""
|
|
311
|
+
|
|
312
|
+
date: Any
|
|
313
|
+
timezone: str | None = None
|
|
314
|
+
|
|
315
|
+
@model_serializer
|
|
316
|
+
def serialize(self) -> dict[str, Any]:
|
|
317
|
+
"""Serialize to MongoDB $dayOfYear expression."""
|
|
318
|
+
if self.timezone is None:
|
|
319
|
+
return {"$dayOfYear": serialize_value(self.date)}
|
|
320
|
+
return {
|
|
321
|
+
"$dayOfYear": {
|
|
322
|
+
"date": serialize_value(self.date),
|
|
323
|
+
"timezone": self.timezone,
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
class HourExpr(ExpressionBase):
|
|
329
|
+
"""
|
|
330
|
+
$hour expression operator - extracts hour (0-23) from date.
|
|
331
|
+
|
|
332
|
+
Example:
|
|
333
|
+
>>> HourExpr(date=F("createdAt")).model_dump()
|
|
334
|
+
{"$hour": "$createdAt"}
|
|
335
|
+
"""
|
|
336
|
+
|
|
337
|
+
date: Any
|
|
338
|
+
timezone: str | None = None
|
|
339
|
+
|
|
340
|
+
@model_serializer
|
|
341
|
+
def serialize(self) -> dict[str, Any]:
|
|
342
|
+
"""Serialize to MongoDB $hour expression."""
|
|
343
|
+
if self.timezone is None:
|
|
344
|
+
return {"$hour": serialize_value(self.date)}
|
|
345
|
+
return {
|
|
346
|
+
"$hour": {
|
|
347
|
+
"date": serialize_value(self.date),
|
|
348
|
+
"timezone": self.timezone,
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
class MinuteExpr(ExpressionBase):
|
|
354
|
+
"""
|
|
355
|
+
$minute expression operator - extracts minute (0-59) from date.
|
|
356
|
+
|
|
357
|
+
Example:
|
|
358
|
+
>>> MinuteExpr(date=F("createdAt")).model_dump()
|
|
359
|
+
{"$minute": "$createdAt"}
|
|
360
|
+
"""
|
|
361
|
+
|
|
362
|
+
date: Any
|
|
363
|
+
timezone: str | None = None
|
|
364
|
+
|
|
365
|
+
@model_serializer
|
|
366
|
+
def serialize(self) -> dict[str, Any]:
|
|
367
|
+
"""Serialize to MongoDB $minute expression."""
|
|
368
|
+
if self.timezone is None:
|
|
369
|
+
return {"$minute": serialize_value(self.date)}
|
|
370
|
+
return {
|
|
371
|
+
"$minute": {
|
|
372
|
+
"date": serialize_value(self.date),
|
|
373
|
+
"timezone": self.timezone,
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
class SecondExpr(ExpressionBase):
|
|
379
|
+
"""
|
|
380
|
+
$second expression operator - extracts second (0-60) from date.
|
|
381
|
+
|
|
382
|
+
Example:
|
|
383
|
+
>>> SecondExpr(date=F("createdAt")).model_dump()
|
|
384
|
+
{"$second": "$createdAt"}
|
|
385
|
+
"""
|
|
386
|
+
|
|
387
|
+
date: Any
|
|
388
|
+
timezone: str | None = None
|
|
389
|
+
|
|
390
|
+
@model_serializer
|
|
391
|
+
def serialize(self) -> dict[str, Any]:
|
|
392
|
+
"""Serialize to MongoDB $second expression."""
|
|
393
|
+
if self.timezone is None:
|
|
394
|
+
return {"$second": serialize_value(self.date)}
|
|
395
|
+
return {
|
|
396
|
+
"$second": {
|
|
397
|
+
"date": serialize_value(self.date),
|
|
398
|
+
"timezone": self.timezone,
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
class MillisecondExpr(ExpressionBase):
|
|
404
|
+
"""
|
|
405
|
+
$millisecond expression operator - extracts milliseconds (0-999).
|
|
406
|
+
|
|
407
|
+
Example:
|
|
408
|
+
>>> MillisecondExpr(date=F("createdAt")).model_dump()
|
|
409
|
+
{"$millisecond": "$createdAt"}
|
|
410
|
+
"""
|
|
411
|
+
|
|
412
|
+
date: Any
|
|
413
|
+
timezone: str | None = None
|
|
414
|
+
|
|
415
|
+
@model_serializer
|
|
416
|
+
def serialize(self) -> dict[str, Any]:
|
|
417
|
+
"""Serialize to MongoDB $millisecond expression."""
|
|
418
|
+
if self.timezone is None:
|
|
419
|
+
return {"$millisecond": serialize_value(self.date)}
|
|
420
|
+
return {
|
|
421
|
+
"$millisecond": {
|
|
422
|
+
"date": serialize_value(self.date),
|
|
423
|
+
"timezone": self.timezone,
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
class WeekExpr(ExpressionBase):
|
|
429
|
+
"""
|
|
430
|
+
$week expression operator - extracts week number (0-53).
|
|
431
|
+
|
|
432
|
+
Example:
|
|
433
|
+
>>> WeekExpr(date=F("createdAt")).model_dump()
|
|
434
|
+
{"$week": "$createdAt"}
|
|
435
|
+
"""
|
|
436
|
+
|
|
437
|
+
date: Any
|
|
438
|
+
timezone: str | None = None
|
|
439
|
+
|
|
440
|
+
@model_serializer
|
|
441
|
+
def serialize(self) -> dict[str, Any]:
|
|
442
|
+
"""Serialize to MongoDB $week expression."""
|
|
443
|
+
if self.timezone is None:
|
|
444
|
+
return {"$week": serialize_value(self.date)}
|
|
445
|
+
return {
|
|
446
|
+
"$week": {
|
|
447
|
+
"date": serialize_value(self.date),
|
|
448
|
+
"timezone": self.timezone,
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
class IsoWeekExpr(ExpressionBase):
|
|
454
|
+
"""
|
|
455
|
+
$isoWeek expression operator - extracts ISO week number (1-53).
|
|
456
|
+
|
|
457
|
+
Example:
|
|
458
|
+
>>> IsoWeekExpr(date=F("createdAt")).model_dump()
|
|
459
|
+
{"$isoWeek": "$createdAt"}
|
|
460
|
+
"""
|
|
461
|
+
|
|
462
|
+
date: Any
|
|
463
|
+
timezone: str | None = None
|
|
464
|
+
|
|
465
|
+
@model_serializer
|
|
466
|
+
def serialize(self) -> dict[str, Any]:
|
|
467
|
+
"""Serialize to MongoDB $isoWeek expression."""
|
|
468
|
+
if self.timezone is None:
|
|
469
|
+
return {"$isoWeek": serialize_value(self.date)}
|
|
470
|
+
return {
|
|
471
|
+
"$isoWeek": {
|
|
472
|
+
"date": serialize_value(self.date),
|
|
473
|
+
"timezone": self.timezone,
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
class IsoWeekYearExpr(ExpressionBase):
|
|
479
|
+
"""
|
|
480
|
+
$isoWeekYear expression operator - extracts ISO week year.
|
|
481
|
+
|
|
482
|
+
Example:
|
|
483
|
+
>>> IsoWeekYearExpr(date=F("createdAt")).model_dump()
|
|
484
|
+
{"$isoWeekYear": "$createdAt"}
|
|
485
|
+
"""
|
|
486
|
+
|
|
487
|
+
date: Any
|
|
488
|
+
timezone: str | None = None
|
|
489
|
+
|
|
490
|
+
@model_serializer
|
|
491
|
+
def serialize(self) -> dict[str, Any]:
|
|
492
|
+
"""Serialize to MongoDB $isoWeekYear expression."""
|
|
493
|
+
if self.timezone is None:
|
|
494
|
+
return {"$isoWeekYear": serialize_value(self.date)}
|
|
495
|
+
return {
|
|
496
|
+
"$isoWeekYear": {
|
|
497
|
+
"date": serialize_value(self.date),
|
|
498
|
+
"timezone": self.timezone,
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
class IsoDayOfWeekExpr(ExpressionBase):
|
|
504
|
+
"""
|
|
505
|
+
$isoDayOfWeek expression operator - extracts ISO day of week (1=Mon, 7=Sun).
|
|
506
|
+
|
|
507
|
+
Example:
|
|
508
|
+
>>> IsoDayOfWeekExpr(date=F("createdAt")).model_dump()
|
|
509
|
+
{"$isoDayOfWeek": "$createdAt"}
|
|
510
|
+
"""
|
|
511
|
+
|
|
512
|
+
date: Any
|
|
513
|
+
timezone: str | None = None
|
|
514
|
+
|
|
515
|
+
@model_serializer
|
|
516
|
+
def serialize(self) -> dict[str, Any]:
|
|
517
|
+
"""Serialize to MongoDB $isoDayOfWeek expression."""
|
|
518
|
+
if self.timezone is None:
|
|
519
|
+
return {"$isoDayOfWeek": serialize_value(self.date)}
|
|
520
|
+
return {
|
|
521
|
+
"$isoDayOfWeek": {
|
|
522
|
+
"date": serialize_value(self.date),
|
|
523
|
+
"timezone": self.timezone,
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
class DateFromPartsExpr(ExpressionBase):
|
|
529
|
+
"""
|
|
530
|
+
$dateFromParts expression operator - constructs date from parts.
|
|
531
|
+
|
|
532
|
+
Example:
|
|
533
|
+
>>> DateFromPartsExpr(year=2024, month=1, day=15).model_dump()
|
|
534
|
+
{"$dateFromParts": {"year": 2024, "month": 1, "day": 15}}
|
|
535
|
+
"""
|
|
536
|
+
|
|
537
|
+
year: Any
|
|
538
|
+
month: Any | None = None
|
|
539
|
+
day: Any | None = None
|
|
540
|
+
hour: Any | None = None
|
|
541
|
+
minute: Any | None = None
|
|
542
|
+
second: Any | None = None
|
|
543
|
+
millisecond: Any | None = None
|
|
544
|
+
timezone: str | None = None
|
|
545
|
+
# ISO week date fields
|
|
546
|
+
iso_week_year: Any | None = None
|
|
547
|
+
iso_week: Any | None = None
|
|
548
|
+
iso_day_of_week: Any | None = None
|
|
549
|
+
|
|
550
|
+
def _add_date_parts(self, result: dict[str, Any]) -> None:
|
|
551
|
+
"""Add date part fields to result dict."""
|
|
552
|
+
if self.iso_week_year is not None:
|
|
553
|
+
result["isoWeekYear"] = serialize_value(self.iso_week_year)
|
|
554
|
+
if self.iso_week is not None:
|
|
555
|
+
result["isoWeek"] = serialize_value(self.iso_week)
|
|
556
|
+
if self.iso_day_of_week is not None:
|
|
557
|
+
result["isoDayOfWeek"] = serialize_value(self.iso_day_of_week)
|
|
558
|
+
else:
|
|
559
|
+
result["year"] = serialize_value(self.year)
|
|
560
|
+
if self.month is not None:
|
|
561
|
+
result["month"] = serialize_value(self.month)
|
|
562
|
+
if self.day is not None:
|
|
563
|
+
result["day"] = serialize_value(self.day)
|
|
564
|
+
|
|
565
|
+
def _add_time_parts(self, result: dict[str, Any]) -> None:
|
|
566
|
+
"""Add time part fields to result dict."""
|
|
567
|
+
if self.hour is not None:
|
|
568
|
+
result["hour"] = serialize_value(self.hour)
|
|
569
|
+
if self.minute is not None:
|
|
570
|
+
result["minute"] = serialize_value(self.minute)
|
|
571
|
+
if self.second is not None:
|
|
572
|
+
result["second"] = serialize_value(self.second)
|
|
573
|
+
if self.millisecond is not None:
|
|
574
|
+
result["millisecond"] = serialize_value(self.millisecond)
|
|
575
|
+
if self.timezone is not None:
|
|
576
|
+
result["timezone"] = self.timezone
|
|
577
|
+
|
|
578
|
+
@model_serializer
|
|
579
|
+
def serialize(self) -> dict[str, Any]:
|
|
580
|
+
"""Serialize to MongoDB $dateFromParts expression."""
|
|
581
|
+
result: dict[str, Any] = {}
|
|
582
|
+
self._add_date_parts(result)
|
|
583
|
+
self._add_time_parts(result)
|
|
584
|
+
return {"$dateFromParts": result}
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
class DateToPartsExpr(ExpressionBase):
|
|
588
|
+
"""
|
|
589
|
+
$dateToParts expression operator - extracts all date parts.
|
|
590
|
+
|
|
591
|
+
Example:
|
|
592
|
+
>>> DateToPartsExpr(date=F("createdAt")).model_dump()
|
|
593
|
+
{"$dateToParts": {"date": "$createdAt"}}
|
|
594
|
+
"""
|
|
595
|
+
|
|
596
|
+
date: Any
|
|
597
|
+
timezone: str | None = None
|
|
598
|
+
iso8601: bool | None = None
|
|
599
|
+
|
|
600
|
+
@model_serializer
|
|
601
|
+
def serialize(self) -> dict[str, Any]:
|
|
602
|
+
"""Serialize to MongoDB $dateToParts expression."""
|
|
603
|
+
result: dict[str, Any] = {"date": serialize_value(self.date)}
|
|
604
|
+
if self.timezone is not None:
|
|
605
|
+
result["timezone"] = self.timezone
|
|
606
|
+
if self.iso8601 is not None:
|
|
607
|
+
result["iso8601"] = self.iso8601
|
|
608
|
+
return {"$dateToParts": result}
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
class DateTruncExpr(ExpressionBase):
|
|
612
|
+
"""
|
|
613
|
+
$dateTrunc expression operator - truncates date to specified unit.
|
|
614
|
+
|
|
615
|
+
Example:
|
|
616
|
+
>>> DateTruncExpr(date=F("timestamp"), unit="day").model_dump()
|
|
617
|
+
{"$dateTrunc": {"date": "$timestamp", "unit": "day"}}
|
|
618
|
+
"""
|
|
619
|
+
|
|
620
|
+
date: Any
|
|
621
|
+
unit: str
|
|
622
|
+
bin_size: int | None = None
|
|
623
|
+
timezone: str | None = None
|
|
624
|
+
start_of_week: str | None = None
|
|
625
|
+
|
|
626
|
+
@model_serializer
|
|
627
|
+
def serialize(self) -> dict[str, Any]:
|
|
628
|
+
"""Serialize to MongoDB $dateTrunc expression."""
|
|
629
|
+
result: dict[str, Any] = {
|
|
630
|
+
"date": serialize_value(self.date),
|
|
631
|
+
"unit": self.unit,
|
|
632
|
+
}
|
|
633
|
+
if self.bin_size is not None:
|
|
634
|
+
result["binSize"] = self.bin_size
|
|
635
|
+
if self.timezone is not None:
|
|
636
|
+
result["timezone"] = self.timezone
|
|
637
|
+
if self.start_of_week is not None:
|
|
638
|
+
result["startOfWeek"] = self.start_of_week
|
|
639
|
+
return {"$dateTrunc": result}
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
__all__ = [
|
|
643
|
+
"DateAddExpr",
|
|
644
|
+
"DateSubtractExpr",
|
|
645
|
+
"DateDiffExpr",
|
|
646
|
+
"DateToStringExpr",
|
|
647
|
+
"DateFromStringExpr",
|
|
648
|
+
"ToDateExpr",
|
|
649
|
+
"YearExpr",
|
|
650
|
+
"MonthExpr",
|
|
651
|
+
"DayOfMonthExpr",
|
|
652
|
+
"DayOfWeekExpr",
|
|
653
|
+
"DayOfYearExpr",
|
|
654
|
+
"HourExpr",
|
|
655
|
+
"MinuteExpr",
|
|
656
|
+
"SecondExpr",
|
|
657
|
+
"MillisecondExpr",
|
|
658
|
+
"WeekExpr",
|
|
659
|
+
"IsoWeekExpr",
|
|
660
|
+
"IsoWeekYearExpr",
|
|
661
|
+
"IsoDayOfWeekExpr",
|
|
662
|
+
"DateFromPartsExpr",
|
|
663
|
+
"DateToPartsExpr",
|
|
664
|
+
"DateTruncExpr",
|
|
665
|
+
]
|