Skip to content

fix: sort top questions by upvote count#64

Open
saurabhhhcodes wants to merge 1 commit into
Ashmita1206:mainfrom
saurabhhhcodes:fix/top-sort-upvote-count-57
Open

fix: sort top questions by upvote count#64
saurabhhhcodes wants to merge 1 commit into
Ashmita1206:mainfrom
saurabhhhcodes:fix/top-sort-upvote-count-57

Conversation

@saurabhhhcodes

Copy link
Copy Markdown

Summary

  • make the questions feed sort=votes path sort by computed upvote count
  • avoid MongoDB array-value sorting on votes.upvotes voter objects
  • add a focused regression test for the top-sort aggregation pipeline

Root Cause

The route previously sorted Top questions with { 'votes.upvotes': -1 }. votes.upvotes is an array of vote objects, so MongoDB sorts by array element values instead of the number of votes. That can rank a one-vote question above a higher-voted question depending on voter ObjectIds.

Changes Made

  • added buildTopQuestionsPipeline() to compute upvoteCountForSort using $size
  • use the aggregation path only for sort=votes; other sort modes keep the existing find().sort() behavior
  • populate authors after aggregation so the API response shape stays consistent

Testing

  • npm ci
  • npm test -- --runTestsByPath tests/question-query-service.test.js
  • node --check server/routes/questions.js && node --check server/services/questionQueries.js && node --check tests/question-query-service.test.js
  • git diff --check

Note

npm ci completed with existing dependency audit/deprecation warnings. This PR does not change dependency files.

Fixes #57

@vercel

vercel Bot commented Jun 5, 2026

Copy link
Copy Markdown

Someone is attempting to deploy a commit to the anglenobida003-8112's projects Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: "Top" sort orders questions incorrectly sorts by voter ID, not vote count

1 participant