K. D.
K. D.

Reputation: 4219

Mongoose explicitly include a "hidden" field

for security reasons I've added select: false to the User's password field, so that it will not be queried by default. However, I'd like to know how to expliclity include it when querying the user on login requests.

My code looks like this right now:

User.findOne({email: 'abc'})
  .then(user => {
    ...
  }).catch(next);

Thank you in advance.

Upvotes: 0

Views: 825

Answers (1)

Shivam Pandey
Shivam Pandey

Reputation: 3936

You can override the select: false at query level as mentioned in the select property docs in the mongoose,

  /**
     * Sets default select() behaviour for this path.
     * Set to true if this path should always be included in the results, false
     * if it should be excluded by default. This setting can be overridden at
     * the query level.
     */
    select?: boolean | any;

So this can be done with select('+password') field, I have tested it and it's working.

Upvotes: 1

Related Questions