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

136
Vistas
How to get the first item of an array field I get with populate in mongoose?

I have a mongodb collection and I want to populate a nested field when I return data from the database. I want to return only specific fields, the code below with explain more.

this is my schema

const hallSchema = new Schema({
  hallName: { type: String, required: true },
  email: { type: String, required: true },
  images: [{ type: String, required: true }],
});

and this is code I'm writing to get the first image of the images array 

chatRooms = await ChatRoom.find({ _id: convertedIds })
      .populate("hallId", `hallName ${images[0]}`);

the query above is failing because it is invalid, how can I get the first item of images array? Thanks for any help

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

What I understand is that you want to populate data in hallId and from populated hallId data you want to select the first image from the images array. So to project fields from populated data you can do this, It works for me,

const chatRooms = await ChatRoom.find({ _id: convertedIds })
    .populate({ path: 'hallId', select: {
        hallName: 1,
        images: { $slice: ['$images', 1] },
    }
});
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