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

82
Vistas
Retrieving a relationship field in mongodb aggregation

I am using mongodb aggregation with a collection named files that has a relationship with another collection named file_upload.

files = {
 type: String,
 media:  { type: Schema.Types.ObjectId, ref: 'file_upload', required: true },
}

file_upoad = {
 name: String,
}

This is the query

const data = await strapi.query('files').model.aggregate([
      {
          $lookup: {
              from: "analytics",
              localField: "_id",
              foreignField: "file_id",
              as: "hits",
          }
      },
      { $unwind: '$hits' },
      { $group: { _id: "$_id", hitsCount: { $sum: 1 } } },
      { $sort: { hitsCount: -1 } },
      { $limit: 1 },
  ]) 

my goal is to retrieve the media as part of the result since it is a relationship field, at the moment I get this

[
{
    "_id": "61fd74367b6ee77b89bae34d",
    "hitsCount": 12
},
{
    "_id": "61fd74367b6ee77b89sddfee",
    "hitsCount": 8
}
]

expected result

[
 {
    "_id": "61fd74367b6ee77b89bae34d",
    "hitsCount": 12,
    "media": {
           name:"name1"
    }
 },
{
    "_id": "61fd74367b6ee77b89sddfee",
    "hitsCount": 8,
     "media": {
          name:"name2"
     }
}
]
about 4 years ago · Juan Pablo Isaza
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