> To build or manage a Wix site with an AI agent, follow https://www.wix.com/headless/skill.md
> It builds a new Wix site or puts your own site online, from a prompt, and keeps changing it afterwards. The pages are code in any web technology, and Wix runs the backend.
> It works through the Wix plugin (the Wix MCP and the Wix skills), the Wix CLI or no Wix login, and puts the site live on Wix hosting or on another host.
> Business features include a store, bookings, events and tickets, a restaurant, a blog, forms, members and many more.
> Read the Wix skills online at https://www.wix.com/skills
> 
> Portal Navigation:
> 
> - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version.
> - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages).
> - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`).
> - If a reference page seems to be missing its schema (parameters, return types, or type definitions), append `?schema=true` to its `.md` URL (e.g. `https://dev.wix.com/docs/contexts/contexts/stores/category-info/use-category-info-context.md?schema=true`).
> - Top-level index of all portals: https://dev.wix.com/docs/llms.txt
> - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt

# Resource: Items

# Type: GroupStage

# Action: by

# Link: https://dev.wix.com/docs/sdk/data-items-sdk/data/items/group-stage/by.md

## Description: Specifies how to group items in the result.

The `by()` method configures the aggregation pipeline to group items for which the specified `expression` resolves to the same value. The `key` and resolved `expression` determine the result item's unique `_id` property.

> **Note**: When `by()` is not called or when it is called with no parameters, all items are placed in a single group.

## Schema:

```json
 Method: by(expression, key)
 Description: Specifies how to group items in the result.  The `by()` method configures the aggregation pipeline to group items for which the specified `expression` resolves to the same value. The `key` and resolved `expression` determine the result item's unique `_id` property.  > **Note**: When `by()` is not called or when it is called with no parameters, all items are placed in a single group.
 Method parameters:
   param name:  expression | type: Expression | description:  Expression to determine the grouping value. Items with matching values are grouped together. Learn more about using [expressions in aggregation pipelines](https://dev.wix.com/docs/sdk/backend-modules/data/items/wix-data-aggregate-pipeline-expressions/introduction.md). 
   param name:  key | type: string | description:  Name of the field in the result that contains the grouping value. 
 Return type: REFERENCE<GroupStage>
     Method: avg(expression, resultFieldName)
     Description: Calculates the average value across all items in the group based on the specified expression.  The `avg()` method refines the aggregation pipeline to calculate the average value of the specified expression across all items in the group.
     Method parameters:
       param name:  expression | type: Expression | description:  Expression to calculate the group's average value. The expression must resolve to a number. Learn more about using [expressions in aggregation pipelines](https://dev.wix.com/docs/sdk/backend-modules/data/items/wix-data-aggregate-pipeline-expressions/introduction.md). 
       param name:  resultFieldName | type: string | description:  Field to store the result in.  Learn more about [field keys](https://support.wix.com/en/article/cms-formerly-content-manager-about-your-collection-fields#field-id-velo-by-wix-only). 
     Return type: REFERENCE<GroupStage>
     Method: by(expression, key)
     Description: Specifies how to group items in the result.  The `by()` method configures the aggregation pipeline to group items for which the specified `expression` resolves to the same value. The `key` and resolved `expression` determine the result item's unique `_id` property.  > **Note**: When `by()` is not called or when it is called with no parameters, all items are placed in a single group.
     Method parameters:
       param name:  expression | type: Expression | description:  Expression to determine the grouping value. Items with matching values are grouped together. Learn more about using [expressions in aggregation pipelines](https://dev.wix.com/docs/sdk/backend-modules/data/items/wix-data-aggregate-pipeline-expressions/introduction.md). 
       param name:  key | type: string | description:  Name of the field in the result that contains the grouping value. 
     Return type: REFERENCE<GroupStage>
     Method: count(resultFieldName)
     Description: Counts the number of items in the group.  The `count()` method refines the aggregation pipeline to count the number of items in the group.
     Method parameters:
       param name:  resultFieldName | type: string | description:  Field to store the result in.  Learn more about [field keys](https://support.wix.com/en/article/cms-formerly-content-manager-about-your-collection-fields#field-id-velo-by-wix-only). 
     Return type: REFERENCE<GroupStage>
     Method: first(expression, resultFieldName)
     Description: Finds the first item in the group.  The `first()` method refines the aggregation pipeline to find the first item in the group.
     Method parameters:
       param name:  expression | type: Expression | description:  Expression to find the first item in the group. Learn more about using [expressions in aggregation pipelines](https://dev.wix.com/docs/sdk/backend-modules/data/items/wix-data-aggregate-pipeline-expressions/introduction.md). 
       param name:  resultFieldName | type: string | description:  Field to store the result in.  Learn more about [field keys](https://support.wix.com/en/article/cms-formerly-content-manager-about-your-collection-fields#field-id-velo-by-wix-only). 
     Return type: REFERENCE<GroupStage>
     Method: last(expression, resultFieldName)
     Description: Finds the last item in the group.  The `last()` method refines the aggregation pipeline to find the last item in the group.
     Method parameters:
       param name:  expression | type: Expression | description:  Expression to find the last item in the group. Learn more about using [expressions in aggregation pipelines](https://dev.wix.com/docs/sdk/backend-modules/data/items/wix-data-aggregate-pipeline-expressions/introduction.md). 
       param name:  resultFieldName | type: string | description:  Field to store the result in.  Learn more about [field keys](https://support.wix.com/en/article/cms-formerly-content-manager-about-your-collection-fields#field-id-velo-by-wix-only). 
     Return type: REFERENCE<GroupStage>
     Method: max(expression, resultFieldName)
     Description: Finds the maximum value across all items in the group.  The `max()` method refines the aggregation pipeline to find the maximum value of the specified field across all the items in the group.
     Method parameters:
       param name:  expression | type: Expression | description:  Expression to find the group's maximum value. Expression must resolve to a comparable value, such as a number or string. Learn more about using [expressions in aggregation pipelines](https://dev.wix.com/docs/sdk/backend-modules/data/items/wix-data-aggregate-pipeline-expressions/introduction.md). 
       param name:  resultFieldName | type: string | description:  Field to store the result in.  Learn more about [field keys](https://support.wix.com/en/article/cms-formerly-content-manager-about-your-collection-fields#field-id-velo-by-wix-only). 
     Return type: REFERENCE<GroupStage>
     Method: min(expression, resultFieldName)
     Description: Finds the minimum value across all items in the group.  The `min()` method refines the aggregation pipeline to find the minimum value of the specified field across all the items in the group.
     Method parameters:
       param name:  expression | type: Expression | description:  Expression to find the group's minimum value. Expression must resolve to a comparable value, such as a number or string. Learn more about using [expressions in aggregation pipelines](https://dev.wix.com/docs/sdk/backend-modules/data/items/wix-data-aggregate-pipeline-expressions/introduction.md). 
       param name:  resultFieldName | type: string | description:  Field to store the result in.  Learn more about [field keys](https://support.wix.com/en/article/cms-formerly-content-manager-about-your-collection-fields#field-id-velo-by-wix-only). 
     Return type: REFERENCE<GroupStage>
     Method: push(expression, resultFieldName)
     Description: Collects values from all items in the group into an array.  The `push()` method refines the aggregation pipeline to collect values from all items in the group into an array.
     Method parameters:
       param name:  expression | type: Expression | description:  Expression to collect items into an array. Learn more about using [expressions in aggregation pipelines](https://dev.wix.com/docs/sdk/backend-modules/data/items/wix-data-aggregate-pipeline-expressions/introduction.md). 
       param name:  resultFieldName | type: string | description:  Field to store the result in.  Learn more about [field keys](https://support.wix.com/en/article/cms-formerly-content-manager-about-your-collection-fields#field-id-velo-by-wix-only). 
     Return type: REFERENCE<GroupStage>
     Method: sum(expression, resultFieldName)
     Description: Calculates the sum of the specified expression across all items in the group.  The `sum()` method refines the aggregation pipeline to calculate the sum of the specified expression across all items in the group.
     Method parameters:
       param name:  expression | type: Expression | description:  Expression to calculate the group's total sum. Expression must resolve to a number. Learn more about using [expressions in aggregation pipelines](https://dev.wix.com/docs/sdk/backend-modules/data/items/wix-data-aggregate-pipeline-expressions/introduction.md). 
       param name:  resultFieldName | type: string | description:  Field to store the result in.  Learn more about [field keys](https://support.wix.com/en/article/cms-formerly-content-manager-about-your-collection-fields#field-id-velo-by-wix-only). 
     Return type: REFERENCE<GroupStage>

```