You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -144,7 +145,7 @@ See also: [Pagination & Order guide](./pagination-order.md#ordering-with-aggrega
144
145
145
146
## Aliases
146
147
147
-
Every aggregation clause requires an `alias`parameter that specifies which record from graph traversal should be used. To reference fields from related records in aggregations, you need to define aliases in the `where` clause using the `$alias`parameter. By default, the root record has alias `$record`:
148
+
Each aggregation function can specify an `alias`indicating which traversed record to read from. If you omit `alias`, RushDB defaults to the root record alias `$record`. To pull values from related records, introduce aliases in the `where` clause with `$alias`and then reference them in aggregations.
148
149
149
150
```typescript
150
151
{
@@ -164,11 +165,7 @@ Every aggregation clause requires an `alias` parameter that specifies which reco
164
165
// Referencing to root record using '$record' alias
-`field`: string - The field to calculate average for
189
-
-`alias`: string - The record alias to use
186
+
-`alias?`: string - Record alias (defaults to `$record`)
190
187
-`precision?`: number - Optional decimal precision for the result
191
188
192
189
```typescript
@@ -214,7 +211,7 @@ graph LR
214
211
### count
215
212
**Parameters:**
216
213
-`fn`: 'count' - The aggregation function name
217
-
-`alias`: string - The record alias to use
214
+
-`alias?`: string - Record alias (defaults to `$record`)
218
215
-`field?`: string - Optional field to count
219
216
-`unique?`: boolean - Optional flag to count unique values
220
217
@@ -240,7 +237,7 @@ graph LR
240
237
**Parameters:**
241
238
-`fn`: 'max' - The aggregation function name
242
239
-`field`: string - The field to find maximum value from
243
-
-`alias`: string - The record alias to use
240
+
-`alias?`: string - Record alias (defaults to `$record`)
244
241
245
242
```typescript
246
243
{
@@ -264,7 +261,7 @@ graph LR
264
261
**Parameters:**
265
262
-`fn`: 'min' - The aggregation function name
266
263
-`field`: string - The field to find minimum value from
267
-
-`alias`: string - The record alias to use
264
+
-`alias?`: string - Record alias (defaults to `$record`)
268
265
269
266
```typescript
270
267
{
@@ -288,7 +285,7 @@ graph LR
288
285
**Parameters:**
289
286
-`fn`: 'sum' - The aggregation function name
290
287
-`field`: string - The field to calculate sum for
291
-
-`alias`: string - The record alias to use
288
+
-`alias?`: string - Record alias (defaults to `$record`)
292
289
293
290
```typescript
294
291
{
@@ -311,7 +308,7 @@ graph LR
311
308
### collect
312
309
**Parameters:**
313
310
-`fn`: 'collect' - The aggregation function name
314
-
-`alias`: string - The record alias to use
311
+
-`alias?`: string - Record alias (defaults to `$record`)
315
312
-`field?`: string - Optional field to collect (if not provided, collects entire records)
316
313
-`unique?`: boolean - Optional flag to collect unique values only. True by default.
317
314
-`limit?`: number - Optional maximum number of items to collect
@@ -337,6 +334,99 @@ graph LR
337
334
}
338
335
```
339
336
337
+
### timeBucket
338
+
Temporal bucketing for datetime fields. Produces a normalized bucket start `datetime` value you can group by (and then apply other aggregations like `count`, `sum`, etc.).
339
+
340
+
**Parameters:**
341
+
-`fn`: 'timeBucket' – Function name
342
+
-`field`: string – Datetime field to bucket (must be typed as `"datetime"` in the record metadata)
343
+
-`alias?`: string – Record alias to read from (defaults to `$record`)
- Use `'months'` when you need a custom N‑month window size (see `size` below)
346
+
-`size?`: number – Positive integer required only when `granularity: 'months'` (e.g. 2 = bi‑monthly, 3 = quarterly equivalent, 6 = half‑year)
347
+
348
+
**Behavior & Guardrails:**
349
+
- RushDB checks the field's type metadata (`datetime`) before computing the bucket; if it is not a datetime field the bucket value becomes `null`.
350
+
- Bucket value is the start of the interval (e.g. month bucket -> first day of month at 00:00:00, quarter -> first day of the quarter, week uses Neo4j `datetime.truncate('week', ...)`).
351
+
- For `granularity: 'months'` the bucket start month is computed with: `1 + size * floor((month - 1)/size)`.
352
+
- Setting `size: 3` is equivalent to `granularity: 'quarter'`.
353
+
-`quarter` is provided as a semantic shortcut (3‑month periods starting at months 1,4,7,10).
354
+
- Difference example: `quarter` -> buckets start at 1,4,7,10; `months` + `size:4` -> buckets start at 1,5,9 (three 4‑month buckets per year). Use `size:3` for quarter‑like grouping when using the generic mode.
If some records lack the datetime type metadata for the chosen field, their bucket will be `null`. To exclude them, add a `where` condition ensuring the field exists and is properly typed, or post‑filter client side. (A future enhancement could expose `$notNull` filtering on aggregation outputs.)
411
+
412
+
#### Combining with Other Aggregations (Self‑Group)
413
+
You can self‑group just by the bucket and one or more metrics:
0 commit comments