Answer the question
In order to leave comments, you need to log in
Is it possible to speed up the top compilation function?
I created a function that sorts users by decreasing value in the field, and also looks for the position of a person in the top.
const getTop = async (name, userId) => {
const top = await Users.find({ admin: false }, { [name]: 1, id: 1 }).sort({
[`${name}`]: -1,
});
let userPosition = 0;
for (let i = 0; i < top.length; i++) {
const { id } = top[i];
if (id === userId) {
userPosition = i;
break;
}
}
const topUsers = top.slice(0, 10);
return [topUsers, userPosition + 1];
}
Answer the question
In order to leave comments, you need to log in
I will try to be less toxic and not write about 10+ years of experience)
You need to read articles or videos about high performance in mongo or optimize query performance.
In short, do not inflate the collection with unnecessary data and make indexes.
About the optimization.
sorts users by decreasing value in the field, as well as
I immediately say that I was looking on the Internet for how to find out the document number when sorting, but I did not find any information about this
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question