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

182
Visualizações
Mongoose Select Method Not Working As Expected

Trying to simply include a field that has select set to false within the schema. Overriding it with select method is not working for some reason. Exclusion of other fields work but inclusion doesn't. In the following example I'm trying to include the "active_account" field. What could be causing this behavior?

Schema

const Schema = new mongoose.Schema({
    active_account: {
        type: Boolean,
        default: true,
        select: false
    }
})

Function

exports.deactivate_user = async ( req , res , next ) => {
    
    const user = await User.findByIdAndUpdate( req.params.id , { active_account: false } ).select( '+active_account' );
    
    res.status( 200 ).json({
        status: 'Success',
        data: user
    });
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

According to docs;

A.findByIdAndUpdate(id, update, options) // returns Query

So,

 const user = await User.findByIdAndUpdate( 
                                           req.params.id, //id
                                           { active_account: false }, //update
                                           { select: 'active_account', new: true } //options
                                          );

set new = true to get the updated version of data.

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