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

296
Vistas
In Mongoose, how do you merge the results of two different sorts, with a pre-specified number of total results?

I have a Jar collection, and its documents hold red and blue marbles. If I wanted to get the top 5 Jars on the basis of having a lot of red marbles, I would do something like this:

Jar.find({}).sort({red: "desc"}).limit(5).exec((err, results) => { ... });

But what if I wanted to get the top 5 Jars on the basis of having a lot of red OR blue marbles, without duplicates? Is there a way to take the results of two different sorts in this fashion?

So for an example, if I have

[{red: 20, blue: 10}, {red: 10, blue: 3}, {red: 5, blue: 22}, {red: 10, blue: 10}], the top 3 (in descending order) will be

[{red: 5, blue: 22},  // 22 from blue is largest
{red: 20, blue: 10},  // 20 from red is 2nd largest
{red: 10, blue: 10}]  // 10 from red (or 10 from blue) is 3rd largest
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can do this by using a few aggregation stages, consider the following:

db.collection.aggregate([
  {
    "$addFields": {
      maxMarbleCount: {
        $max: [
          "$red",
          "$blue"
        ]
      }
    }
  },
  {
    "$sort": {
      maxMarbleCount: -1
    }
  },
  {
    "$limit": 3
  }
])

Basically I add a temporary field called maxMarbelCount to each document, which is determined by the max red/blue value. You can then (desc)-sort by this field and limit accordingly.

If needed, you can add another projection stage to remove the temporary field from the output .

Here's an example on mongoplayground: https://mongoplayground.net/p/QApaz7YKWy8

over 4 years ago · Santiago Trujillo 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