Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!