@@ -315,11 +315,13 @@ You can pass either one grouping rule or an array of grouping rules.
315315When you use a single grouping rule, the grouping value is returned in ` group ` .
316316When you use several grouping rules, the values are returned in ` group1 ` , ` group2 ` , etc.
317317
318- To use explicit response keys, pass the optional ` as ` argument:
318+ To use explicit response keys that differ from source field names, pass the optional ` as ` argument to the grouping constructor.
319+ For example, if you want the country group to be returned as ` country_name ` instead of ` country ` , pass it as the second ` Field ` argument.
320+ For ` DateTrunc ` , pass the explicit response key as the fourth argument:
319321
320322``` ts
321- GroupBy .Field (' country' , ' country ' )
322- GroupBy .DateTrunc (' created_at' , ' month' , ' Europe/Kyiv' , ' month ' )
323+ GroupBy .Field (' country' , ' country_name ' )
324+ GroupBy .DateTrunc (' created_at' , ' month' , ' Europe/Kyiv' , ' month_name ' )
323325```
324326
325327Example:
@@ -373,8 +375,8 @@ With explicit grouping aliases:
373375``` ts
374376[
375377 {
376- country : string ,
377- month : string ,
378+ country_name : string ,
379+ month_name : string ,
378380 count: number | string ,
379381 uniqueOwners?: number | string ,
380382 minPrice?: number | null ,
@@ -441,18 +443,18 @@ const rows = await admin.resource('apartments').aggregate(
441443 avgPrice: Aggregates .avg (' price' ),
442444 },
443445 [
444- GroupBy .Field (' country' , ' country ' ),
445- GroupBy .DateTrunc (' created_at' , ' month' , ' Europe/Kyiv' , ' month ' ),
446+ GroupBy .Field (' country' , ' country_name ' ),
447+ GroupBy .DateTrunc (' created_at' , ' month' , ' Europe/Kyiv' , ' month_name ' ),
446448 ],
447449);
448450```
449451
450452What is happening here:
451453- [ ] → 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
454+ - GroupBy.Field('country', 'country_name ')
455+ → groups by the ` country ` field and returns the value in the ` country_name ` key
456+ - GroupBy.DateTrunc('created_at', 'month', 'Europe/Kyiv', 'month_name ')
457+ → groups by month and returns the value in the ` month_name ` key
456458- countDistinct('owner_id') → number of unique owners in each group
457459- min('price') and max('price') → price range in each group
458460- the result has one row per country and month combination
0 commit comments