Convex Aggregate Component
The Convex Aggregate component provides efficient aggregation operations on your Convex data with O(log n) time complexity, replacing the O(n) performance of naive.collect() or COUNT(*) queries.
Perfect for leaderboards, analytics dashboards, pagination, and any scenario where you need big-picture data without fetching every document.
This component maintains a denormalized data structure that automatically keeps counts and sums up-to-date as your data changes.
Key Features
Lightning Fast Queries Get counts, sums, rankings, and percentiles in logarithmic time, no matter how large your dataset grows. Flexible Sorting & Grouping Sort by numbers, strings, or compound keys. Group data by prefix to aggregate at any level of detail. Namespace Isolation Separate data into isolated namespaces for maximum throughput when partitions don’t need cross-aggregation. Fully Reactive All aggregates update in real-time with Convex’s reactivity. No polling, no stale data, no refresh needed. Transactional Updates Atomically update your tables and aggregates together. No race conditions or inconsistent states. Batch Operations Perform multiple aggregation queries efficiently in a single call withcountBatch(), sumBatch(), and atBatch().
Common Use Cases
Leaderboards
Track game scores with rankings, percentiles, and user-specific statistics
Analytics Dashboards
Calculate total counts, sums, and averages across time ranges or categories
Offset Pagination
Display numbered pages of results without loading all documents
Random Access
Pick random items efficiently for playlists, recommendations, or sampling
Quick Example
Here’s what you can do with the Aggregate component:How It Works
The Aggregate component maintains an internal tree-based data structure that stores denormalized counts and sums. This allows queries to read just a few documents instead of scanning your entire table.Get Started
Installation
Install the component and configure your Convex app
Quickstart
Build a working leaderboard in minutes
Performance Comparison
| Operation | Without Aggregate | With Aggregate |
|---|---|---|
| Count all items | O(n) | O(log n) |
| Find ranking | O(n) | O(log n) |
| Get item at offset | O(n) | O(log n) |
| Calculate sum | O(n) | O(log n) |
| Find percentile | O(n log n) | O(log n) |
The component is fully transactional and reactive, ensuring your aggregates stay in sync with your data automatically.