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
.