是否可以使用貓鼬模式在模式級別定義基于另一個字段的字段?例如,假設我有一個非常簡單的架構:const mongoose = require('mongoose')const { Schema } = mongooseconst UserSchema = new Schema({ username: { type: String, required: true, unique: true }, username_lower: { type: String, // calculated: this.username.toLowerCase() }, email: { type: String, required: true, unique: true }, email_lower: { // for case-insensitive email indexing/lookup type: String, // calculated: this.email.toLowerCase() }, password: { type: String, required: true }, password_acceptable: { type: Array, // calculated: [ // this.password.toLowerCase(), // this.password.toUpperCase() // this.password.removeWhiteSpace(), // just an example // ] }})const User = mongoose.model('User', UserSchema)module.exports = User是否有類似于虛擬“計算”字段(我已注釋掉)的東西,它允許在保存新文檔時自動創建字段?這將非常方便,并且可以減少必須在我的后端路由上手動定義這些字段的混亂。
是否可以在模式級別定義使用 mongoose 從另一個字段計算的字段?
MMTTMM
2021-10-07 10:35:00