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

202
Visualizações
How populate items to Model in order

I have a question.

How do I sort the items which I add to the Model via .populate by some variable in the data?

The data I get:

"items": [
                {
                    "_id": "xxxx",
                    "name": "Item 1",
                    "price": 4000,
                    "order": 2,
                    "user": {
                        "_id": "xxxx",
                    }
                },
                {
                    "_id": "xxxx",
                    "name": "Item 2",
                    "price": 4000,
                    "order": 1,
                    "user": {
                        "_id": "xxxx",
                    }
                }
            ],

Here I want them to be sorted by the order variable. So Item 1 will be second and Item 2 will be first.

The order variable is not required, so if item does not have order, it should be sorted on last position.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

db.collection.aggregate({
"$sort": {order: 1}
})

you can use -1 for descending order. for live test check below link

mongodb sort result

about 4 years ago · Juan Pablo Isaza Relatório
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