0

So I have documents that follow this schema:

{
    _id: String,
    globalXP: {
        xp: {
            type: Number,
            default: 0
        },
        level: {
            type: Number,
            default: 0
        }
    },
    guilds: [{ _id: String, xp: Number, level: Number }]
}

So basically users have their own global XP and xp based on each guild they are in.

Now I want to make a leaderboard for all the users that have a certain guildID in their document.
What's the most efficient way to fetch all the user documents that have the guild _id in their guilds array and how do I sort them afterwards?

I know it might be messy as hell but bare with me here.

1 Answer 1

2

If I've understand well, you only need this line of code:

var find = await model.find({"guilds._id":"your_guild_id"}).sort({"globalXP.level":-1})

This query will return all documentas where guilds array contains the specific _id and sort by player level.

In this way the best level will be displayed first.

Here is an example how the query works. Please check if it work as you expected.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.