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

306
Visualizações
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 Respostas
Responde à pergunta

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 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