I am attempting to organize MongoDB arrays in a descending order. I have devised some JavaScript code to transform all the documents into JSON arrays, but I also need to arrange them in a descending order. Here is the code I have written:
const result = xpSchema.find({
gid: '754542598957432943',
});
jsonString = JSON.parse(JSON.stringify(result));
console.log(jsonString);
The format of the documents is as follows:
{
xp: 5,
coins: 0,
level: 1,
_id: '5f9337a49b46164777c46905',
gid: '754542598957432943',
profilepic: null,
userId: '712947234886647828',
username: 'CoolUserName',
__v: 0
}
I am looking to sort them based on the XP and Level fields. In case the level matches in different documents, it should consider the XP and prioritize the one with the higher XP. The highest one should be displayed on top.
Do you have any suggestions on how I could achieve this?
Thank you :)