'sort mongodb data based on array express react

i have record in mongoose which kinda look like this(demo) -

{
    name:"son",
    email:"[email protected]",
    score:[40,100,30,5]
}

my model is -

const uModel =  new Schema({
name: {
    type: String,
    required: true,
    max: 20, 
},
email: {
    type: String,
    required: true,
    unique: true,
    max: 50,
    min: 6
},
password: {
    type: String,
    required: true,
    min: 6
},
profile:{
    type: String,
    default: ''
},
score:{
    type: Array
}})

and here is my post method in express -

const getAllData = (req, res)=>{
userModel.find().sort({score: -1})
.then(user =>{
    res.status(200).json(user)

})
.catch(err=>{
    res.status(200).json(err)
})}

i have a dashboard and i want to show user with highest score(score is dynamic, changes all the time) what i want is to sort score array(descending order ) and then sort user based on highest score(descending order). two thing what i want but i have no idea how to do it. after sorting 0 index of score array should have the higher value and last index lowest.

find().sort({}) does not work for me.

is it possible or should i change my strategy if i should change then what could be better one ?? thanks in advance



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source