@@ -300,20 +300,49 @@ This lets you answer questions like:
300300
301301### Available aggregates
302302- Aggregates.count()
303- - Aggregates.avg(field)
303+ - Aggregates.countDistinct(field)
304+ - Aggregates.avg(field)
304305- Aggregates.sum(field)
306+ - Aggregates.min(field)
307+ - Aggregates.max(field)
305308- Aggregates.median(field)
306309
307310### Available grouping
308- - GroupBy.Field(field)
309- - GroupBy.DateTrunc(field, unit, timezone)
311+ - GroupBy.Field(field, as?)
312+ - GroupBy.DateTrunc(field, unit, timezone?, as?)
313+
314+ You can pass either one grouping rule or an array of grouping rules.
315+ When you use a single grouping rule, the grouping value is returned in ` group ` .
316+ When you use several grouping rules, the values are returned in ` group1 ` , ` group2 ` , etc.
317+
318+ To use explicit response keys, pass the optional ` as ` argument:
319+
320+ ``` ts
321+ GroupBy .Field (' country' , ' country' )
322+ GroupBy .DateTrunc (' created_at' , ' month' , ' Europe/Kyiv' , ' month' )
323+ ```
310324
311325Example:
312326``` ts
313327GroupBy .DateTrunc (' created_at' , ' month' , ' Europe/Kyiv' )
314328```
315329
316330### Response format
331+ Without grouping, each row contains only requested aggregate aliases:
332+
333+ ``` ts
334+ [
335+ {
336+ count: number | string ,
337+ avgPrice?: number | null ,
338+ sum?: number | null ,
339+ medianPrice?: number | null ,
340+ }
341+ ]
342+ ```
343+
344+ With one grouping rule:
345+
317346``` ts
318347[
319348 {
@@ -326,6 +355,35 @@ GroupBy.DateTrunc('created_at', 'month', 'Europe/Kyiv')
326355]
327356```
328357
358+ With several grouping rules:
359+
360+ ``` ts
361+ [
362+ {
363+ group1: string ,
364+ group2: string ,
365+ count: number | string ,
366+ avgPrice?: number | null ,
367+ }
368+ ]
369+ ```
370+
371+ With explicit grouping aliases:
372+
373+ ``` ts
374+ [
375+ {
376+ country: string ,
377+ month: string ,
378+ count: number | string ,
379+ uniqueOwners?: number | string ,
380+ minPrice?: number | null ,
381+ maxPrice?: number | null ,
382+ avgPrice?: number | null ,
383+ }
384+ ]
385+ ```
386+
329387### Get daily apartment stats (count, avg, sum, median) for listed apartments
330388``` ts
331389const rows = await admin .resource (' apartments' ).aggregate (
@@ -369,4 +427,32 @@ What is happening here:
369427- [ ] → no filters (all records)
370428- GroupBy.Field('country')
371429→ grouping by country
372- - same aggregates (count, avg, sum, median)
430+ - same aggregates (count, avg, sum, median)
431+
432+ ### Get apartment stats grouped by country and month
433+ ``` ts
434+ const rows = await admin .resource (' apartments' ).aggregate (
435+ [],
436+ {
437+ count: Aggregates .count (),
438+ uniqueOwners: Aggregates .countDistinct (' owner_id' ),
439+ minPrice: Aggregates .min (' price' ),
440+ maxPrice: Aggregates .max (' price' ),
441+ avgPrice: Aggregates .avg (' price' ),
442+ },
443+ [
444+ GroupBy .Field (' country' , ' country' ),
445+ GroupBy .DateTrunc (' created_at' , ' month' , ' Europe/Kyiv' , ' month' ),
446+ ],
447+ );
448+ ```
449+
450+ What is happening here:
451+ - [ ] → no filters (all records)
452+ - GroupBy.Field('country', 'country')
453+ → groups by country and returns the value in the ` country ` key
454+ - GroupBy.DateTrunc('created_at', 'month', 'Europe/Kyiv', 'month')
455+ → groups by month and returns the value in the ` month ` key
456+ - countDistinct('owner_id') → number of unique owners in each group
457+ - min('price') and max('price') → price range in each group
458+ - the result has one row per country and month combination
0 commit comments