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

151
Visualizações
Mongoose - Protect the password field depending on the requested route

Given the following user model:

const UserSchema = new mongoose.Schema({
  id: Number,
  userID: {
    type: String,
    unique: true
  },
  userName: String,
  password: {type: String, select: false}, 
  isAdministrator: {
    type: Boolean,
    default: false
  }
}, {
  timestamps: true
});

This model ensures that the password is not returned in the event of a request.

How do I change the UserModel so that the password is returned for Route A and not for Route B?

For instance:

router.get('/A', [returns password])
router.get('/B', [returns no password])
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In your controller, where you execute your Mongoose Query, for example:

User.findOne({email})

you append a select function and put inside the parethesis the field you want to be selected, for instance:

User.findOne({email}).select("+password")

If you put a "-" it will exclude the field, if you do not put any symbol it will select that field, the "+" forces the selection of the fields that have a "select false" property defined in the schema.

Query.prototype.select() documentation

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