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

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

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