For the encryption and authentication for mongoose documents, we can use a package called mongoose-encryption.
Refer: https://www.npmjs.com/package/mongoose-encryption
Install mongoose-encryption package
npm i mongoose-encryption
Using mongoose-encrytption
const encrypt = require("mongoose-encryption");
const userSchema = new mongoose.Schema{ email: String, password: String }; const secret = "Thisisourlittlesecret"; userSchema.plugin(encrypt, {secret: secret, encryptedFields: ['password']}); const User = new mongoose.model("User", userSchema);