Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

118
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda