Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
sort mongodb data based on array express react

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

{
    name:"son",
    email:"final@gmail.com",
    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

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!