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
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