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

260
Vistas
Counting value of subdocuments in mongoose subquery

I have a collection of documents that look like this:

[
  { group_id: 1, value: 'foo' },
  { group_id: 1, value: 'bar' },
  { group_id: 1, value: 'bar' },
  { group_id: 1, value: 'bar' },
  { group_id: 2, value: 'bar' },
  { group_id: 2, value: 'foo' },
  { group_id: 2, value: 'foo' },
  { group_id: 2, value: 'foo' }
]

For each group_id I want to return the value that occurs the most. So my output should look something like this...

[
  { group_id: 1, maxValue: 'bar', maxValueCount: 3 },
  { group_id: 2, maxValue: 'foo', maxValueCount: 3 }
]

How would you do this using the Mongoose aggregate function?

Update: This is as far as I've gotten, I just need to return the value of the maximum count now...

const records = await Record.aggregate([
  {
    $group: {
        _id: {
            id: '$group_id',
            value: '$value'
    },
    count: { $sum: 1 }
  }
}
])
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You may achieve your goal by

  • grouping by group_id/value (and counting the occurrences of value)
  • then grouping by group_id and pushing all the found values within with their count
  • finally keeping from the array the value having the max count
data=[
  { group_id: 1, value: 'foo' },
  { group_id: 1, value: 'bar' },
  { group_id: 1, value: 'bar' },
  { group_id: 1, value: 'bar' },
  { group_id: 1, value: 'bar' },//one more added
  { group_id: 2, value: 'bar' },
  { group_id: 2, value: 'foo' },
  { group_id: 2, value: 'foo' },
  { group_id: 2, value: 'foo' }
]

db.products.remove({})
db.products.insert(data)

const stages = [
{
  $group: {
    _id: {
      group_id: '$group_id',
      value: '$value'
    },
    n: { $sum: 1 }
  }
},
{
  $group: {
    _id: '$_id.group_id',
    values: {
      $push: {
        value: '$_id.value',
        n: '$n'
      }
    }
  }
},
{
  $project: {
    group_id:1,
    best: {
      $reduce: {
        input: '$values',
        initialValue: { n: 0, value: ''},
        in: {
          $cond: [
            {
              $lt: ['$$value.n', '$$this.n']
            },
            '$$this',
            '$$value'
          ]
        }
      }
    }
  },
},
{
  $project: {
    group_id: 1,
    value: '$best.value',
    maxValue: '$best.n'
  }
}
]
printjson(db.products.aggregate(stages).toArray())

playground

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