Reputation: 63
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.
Upvotes: 0
Views: 112
Reputation: 15187
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.
Upvotes: 2