This quickstart assumes you’ve already installed the Aggregate component.
Define Your Schema
First, create a table to store your data. For this example, we’ll track game scores:convex/schema.ts
Configure Your Aggregate
Create aTableAggregate to track scores. We’ll create two different views:
- Sort by score (for global rankings)
- Group by user and score (for user statistics)
convex/leaderboard.ts
Set Up Automatic Syncing
Use triggers to keep aggregates in sync automatically:convex/leaderboard.ts
Write Data
Now you can insert scores, and the aggregates will update automatically:convex/leaderboard.ts
Query Aggregated Data
Now you can efficiently query your data in O(log n) time:- Total Count
- Score at Rank
- Rank of Score
- Top 10 Scores
User-Specific Statistics
Use the second aggregate to efficiently compute per-user statistics:Try It Out
Add some test data from the Convex Dashboard:Backfilling Existing Data
If you’re adding aggregates to an existing table with data, see the Migrations and Backfills guide for instructions on backfilling existing records.Next Steps
Core Concepts
Learn about keys, sorting, grouping, and namespacing
Use Cases
Explore real-world examples like leaderboards and pagination
API Reference
Browse the complete API documentation
Performance
Optimize for high-throughput workloads