Mongoose SchemaTypes Getters
Mongoose 是在 Node.js 和 MongoDB 中 MongoDB 的一种流行的对象数据建模(ODM)库. Mongoose 可以使用 SchemaTypes 为模式字段定义自定义的获取器和设置器。 获取器 允许您定义一个函数,该函数在从数据库返回模式字段的原始值之前将其转换。这对于在 MongoDB 中格式化、解析或加密存储的数据非常有用。
参数: 为 SchemaType 定义的获取器函数应该接受一个参数和要转换的字段的值。该函数可以返回任何应该用作字段的转换值的值。如果函数返回 undefined,则将使用字段的原始值。
语法:
const mySchema = new mongoose.Schema({
field: {
type: String,
get: function(value) {
// Transform and return value here
return transformedValue;
}
}
});
示例1: 在这里,我们使用javascript内置函数toUpperCase。当用户输入任何小写或大写字母时,在其传递到MongoDB之前,它会自动转换为大写字母。
// Import the mongoose
const mongoose = require('mongoose');
// User Schema
// Define the schema for the "Geek" object
const geekSchema = new mongoose.Schema({
name: {
// The "name" field is of type "String"
type: String,
// A getter function that returns the
// value of the "name" field in uppercase
get: (name) => name.toUpperCase()
}
});
// User model
// Create a model for the "Geek" object
// using the "geekSchema" schema
const Geek = mongoose.model('Geek', geekSchema);
// Create a new "Geek" object with the "name"
// field set to "geeksforgeeks"
const geek = new Geek({ name: 'geeksforgeeks' });
// Log the value of the "name" field on the console
console.log(geek.name); // Output: "GEEKSFORGEEKS"
输出:
示例2: 在这里,我们制作了一个包含邮件和密码的模式,其中我们尝试隐藏邮件并加密密码。为了隐藏邮件,我们将建立自己的函数,并使用md5散列。首先,我们必须在应用程序中使用命令提示符中的命令安装md5。
npm i md5
// Import required dependencies
// Import Mongoose for defining the
// schema and model
const mongoose = require('mongoose');
// Import MD5 for encrypting the password
const md5 = require('md5');
// Define the user schema
const userSchema = new mongoose.Schema({
email: {
// Set the data type to string
type: String,
// Define a getter function for email field
get: (email) => {
// Initialize a variable for storing
// the masked email
let masked = "";
// Get the actual email value
let mailId = email;
// Get the prefix part of email
let prefix = mailId.substring(0,
mailId.lastIndexOf("@"));
// Get the postfix part of email
let postfix = mailId.substring(
mailId.lastIndexOf("@"));
// Loop through each character of prefix
for (let i = 0; i < prefix.length; i++) {
// Keep first and last characters unmasked
if (i == 0 || i == prefix.length - 1) {
// Append the unmasked character
// to masked string
masked = masked + prefix[i].toString();
}
else {
// Mask all other characters with '*'
masked = masked + "*";
}
}
// Add the postfix to masked string
masked = masked + postfix;
return masked; // Return the masked email
}
},
password: {
// Set the data type to string
type: String,
// Define a getter function for password
// field to encrypt the value using MD5
get: (type) => md5(type)
}
});
// Create a user model from the schema
const User = mongoose.model('User', userSchema);
// Create a new user instance with email and password
const user = new User({
email: 'abc1452@gmail.com',
password: '1234'
});
// Log the masked email and encrypted
// password of the user
// Output: a******@gmail.com
console.log(user.email);
// Output: 81dc9bdb52d04dc20036dbd8313ed055
console.log(user.password);
输出: