Overview
TableAggregate connects your Convex tables to the Aggregate component, automatically calculating counts and sums based on your table data. It defines how table data will be sorted and summed in the aggregate data structure.Creating a TableAggregate
Define a TableAggregate with type parameters specifying the key type, data model, and table name:Configuration Options
sortKey (required)
Defines how documents are sorted in the aggregate. The key can be any Convex value:sumValue (optional)
Defines what value to aggregate when calling.sum(). If not provided, sum operations will return 0:
namespace (optional)
Partitions data into separate namespaces for independent aggregation and better throughput:Leaderboard Example
Here’s a complete example for a game leaderboard:Grouped Aggregation
Use tuple keys to group data and query at different levels:Querying Operations
Once your TableAggregate is configured, you can perform various aggregation queries:Namespaces vs. Grouped Keys
Use Namespaces when:
- You never need to aggregate across groups
- You want maximum write throughput
- Data partitions are completely independent
Use Grouped Keys when:
- You need both grouped and global aggregation
- You want to query across all groups
- Lower write throughput is acceptable
Next Steps
- Learn about Direct Aggregates for non-table data
- Discover Batch Operations for better performance
- Set up Keeping Data in Sync with triggers