Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

200
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda