Answer the question
In order to leave comments, you need to log in
How to filter documents with the highest values?
Hello, there is a task: to take the top 20 by the value of the user document. Each user has a "points" value that stores a number. As I said, you need to take the top 20 users with the highest values in "points". However, I did not find an operator for such a selection.
Answer the question
In order to leave comments, you need to log in
Sort in descending order points and limit the withdrawal to no more than the first 20
var players = [
{
id: 1,
points: 14
},
{
id: 2,
points: 8
},
{
id: 3,
points: 36
}
]; // json для примера. если монго, то нужно просто получить массив со всеми игроками
players.sort(function (a, b) {
if(b.points > a.points) return 1;
if(b.points < a.points) return -1;
return 0;
}); // сортировка
var top = [];
for (var i = 0; i < 20; i++) {
if(players[i]) top.push(players[i]);
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question