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

142
Visualizações
Is there a way to filter this request with mongoose?

I need to make a query of all products but only with the category status in true,

The model of my products is something like this:

const ProductSchema = Schema({
name : {type: String},
category: {type: Schema.Types.ObjectId, ref: 'Category'}
})

Normally I would just make a query and then a filter for the products with category.

status === true, but the thing is that I'm using pagination and due to the filter my page of 15 products ends up with fewer products than 15 obviously because of the filter, so I really don't know if I can make the filter with a population in the query, maybe something like this:

const query = await Product.find().populate('category', 'name status')
.select(['-cost']).where('category.status')
.equals(true).skip(0).limit(15)

But sadly it doesnt work, is there a way to do it or should I give up?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can easily achieve this with an aggregation:

const query = await Product.aggregate([
      {
        $lookup: {
          from: 'Category',
          localField: 'category',
          foreignField: '_id',
          as: 'status'
        }
      },
      {
        $match: {
          "status.status": true
        }
      },
      {
        $project: {
          "cost": 0, // disclude
        }
      },
      {
        $limit: 15
      },
      {
        $skip: 0
      }
  ])
// passed the tests in my database
about 4 years ago · Juan Pablo Isaza 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