Sometimes, we want to protect the password field in MongoDB Mongoose so it won’t return in a query.
In this article, we’ll look at how to protect the password field in MongoDB Mongoose so it won’t return in a query.
How to protect the password field in MongoDB Mongoose so it won’t return in a query?
To protect the password field in MongoDB Mongoose so it won’t return in a query, we can set the select option to false on a field when we create the schema.
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 the userSchema that has the password field set to type String.
And we add select and set it to false to prevent it from being returned in queries.
Conclusion
To protect the password field in MongoDB Mongoose so it won’t return in a query, we can set the select option to false on a field when we create the schema.