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

138
Vistas
Mongoose Get only size of a array

I'm using Mongoose and a MongoDB. I have three schemas user and genres and songs.

const userSchema = new Schema({
  name: String,
  age: String,
  favorite_genres: {
    type: Schema.Types.ObjectId,
    ref: 'Genre'
  }
});

const genreSchema = new Schema({
  name: String,
  songs: {
    type: Schema.Types.ObjectId,
    ref: 'Song'
  }
});

const songSchema = new Schema({
  title: String,
  artist: String,
  length: String,
  year: String
});

those are simplifid schemas, but they will do for this puprose. So when I query a user I want to populate the doc with the favorite genres. But there (due to the potential amount of data) not with the ids of all songs, but only the number of songs. How can I achieve that?

I tried virtuals but they don't work the way I wanted it. What I'm looking for is sth like the following:

const user = await User.findById(userID).populate({path: 'favorite_genres', select: 'name', sizeof: 'songs'}).lean()

// or

const user = await User.findById(userID).populate({path: 'favorite_genres', select: ['name', 'songs.length']}).lean()

Does anyone have an idea?

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

Look at the link I attached. It's a similar question, I think You will find Your answer over there. You can find Your answer here

about 4 years ago · Santiago Gelvez 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