Muhammad Tabarak - Home
Backend Engineering

Optimizing Review Aggregation Scoring Pipelines in MongoDB

Scaling review scoring and Bayesian sentiment aggregation pipelines in MongoDB from O(N) full-scans to O(1) pre-aggregated bucket patterns.

Muhammad Tabarak
Muhammad TabarakFounder & Software Engineer
November 6, 2026
3 min read
Optimizing Review Aggregation Scoring Pipelines in MongoDB

Calculating accurate average ratings across millions of customer reviews seems trivial on paper: run an aggregation pipeline with $match and $group: { _id: '$companyId', avgRating: { $avg: '$rating' } }. In high-traffic directories like Trusts Advisor, however, calculating this on the fly during search queries produced severe performance degradation.

As the review database grew beyond 2,000,000 documents, every company profile visit triggered full index index scans and memory-intensive aggregation stages, causing database CPU utilization to spike to 95%. Here is how we redesigned the scoring pipeline using the Bucket Pattern and Bayesian Weighted Averages to achieve sub-5ms response times.

The Scaling Trap of Real-Time Aggregation

In standard relational and document models, calculating a company's rating involves scanning every individual review ever written for that business. For popular firms with tens of thousands of reviews, MongoDB had to fetch and compute arithmetic sums across megabytes of raw documents on every page load.

Pipeline Benchmark
Rendering interactive flow...
Figure: Performance Comparison: Real-Time $group vs Pre-Aggregated Bucket

The Bayesian Weighted Score: Preventing New-Listing Skew

A simple arithmetic average creates a severe credibility problem: a newly listed company with one single 5-star review would outrank a reputable enterprise with 10,000 reviews and a 4.9-star rating. To eliminate this distortion, Trusts Advisor employs a Bayesian Weighted Score algorithm.

The Bayesian formula pulls low-volume businesses toward the global directory average, requiring a solid volume of verified feedback before a company can achieve a top-tier platform rank.

Data Architecture
Rendering interactive flow...
Figure: Write-Time Incremental Bucket Update Pipeline

Measurable Database Optimization Gains

  • 99.4% Latency Reduction: Search result card generation plummeted from 850ms to an average of 4.2ms by transforming an expensive aggregation query into an indexed primary key lookup.
  • Flat Database CPU Usage: Average database CPU utilization dropped from 88% during peak hours to under 12%, allowing the infrastructure to scale to 10x traffic without provisioning larger VPS instances.
  • Instant Category Filtering: Because the aggregate rating, star breakdown, and Bayesian score reside directly on the pre-computed document, sorting companies by rank requires zero runtime computations.
Enjoyed this article?Add this site as a preferred source so Google surfaces my posts first — in Search & AI Mode.
Add as preferred source→