Skip to content

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

@samyatiwarii

Description

@samyatiwarii

In server/routes/questions.js, the "top" sort uses:
jssortOption = { 'votes.upvotes': -1, createdAt: -1 }
But votes.upvotes is an array of user ObjectIDs, not a number. MongoDB doesn't sort arrays by length — it sorts by the first element's value. So a question with 10 votes can appear below one with 1 vote depending on who voted first.
To reproduce:

Create two questions, upvote one 10 times and the other once
Switch feed to "Top" sort
The ordering won't reliably put the higher-voted question first

Fix: Sort by $size of the upvotes array using aggregation, or add a stored upvoteCount field to the schema and keep it in sync on every vote.
Files: server/routes/questions.js (~line 92), server/models/Question.js

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions