Categories
JavaScript Answers

How to protect the password field in Mongoose/MongoDB so it won’t return in a query when populating collections with JavaScript?

Spread the love

To protect the password field in Mongoose/MongoDB so it won’t return in a query when populating collections with JavaScript, we set the select option to false.

For instance, we write

const userSchema = new Schema({
  name: { type: String, required: false, minlength: 5 },
  email: { type: String, required: true, minlength: 5 },
  phone: String,
  password: { type: String, select: false },
});

to create a schema with the password field having the select option set to false.

Then the field’s value won’t be returned when we query the userSchema.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *