英文:
How to add more fields in mongoDB database schema based on other field
问题
以下是您要翻译的代码部分:
const mongoose = require("mongoose");
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
role: {
type: String,
default: "user",
},
});
module.exports = mongoose.model("USER", userSchema);
例如,这是用户模式。现在,如果在注册用户时,如果 user.role == "doctor"
,那么模式应该是:
const mongoose = require("mongoose");
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
role: {
type: String,
default: "user",
},
fees: {
type: Number,
default: 1000,
},
specialization: {
type: String,
default: "Physicist",
},
location: {
type: String,
default: "India",
},
});
module.exports = mongoose.model("USER", userSchema);
或者如果 user.role == "user"
,那么模式应该是原始的,不包含 fees
、specialization
、location
字段。
我尝试使用了 mongoose.pre
函数,但它没有生效。以下是示例:
userSchema.pre("save", function (next) {
if (this.role === "doctor") {
this.fees = 1000;
this.specialization = "Physicist";
this.location = "India";
}
next();
});
请注意,这些代码是基于您提供的原始内容翻译而来的。如果您有任何其他疑问或需要进一步的帮助,请告诉我。
英文:
const mongoose = require("mongoose");
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
role: {
type: String,
default: "user",
},
});
module.exports = mongoose.model("USER", userSchema);
For example this is user schema. Now if while registering a user, then if the user.role == "doctor"
then the schema should be
const mongoose = require("mongoose");
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
role: {
type: String,
default: "user",
},
fees: {
type: Number,
default: 1000,
},
specialization: {
type: String,
default: "Physicist",
},
location: {
type: String,
default: "India",
},
});
module.exports = mongoose.model("USER", userSchema);
or if the user.role == "user"
then the schema should be normal original one without the fields such as fees, specializatoin, location
I tried using mongoose.pre function but it didnt work. This is example
userSchema.pre("save", function (next) {
if (this.role === "doctor") {
this.fees = 1000;
this.specialization = "Physicist";
this.location= "India";
}
next();
});
答案1
得分: 1
为了根据用户模式中的角色属性实现有条件的模式字段,您可以使用Mongoose的判别器。判别器允许您基于共同的父模式来定义不同的模式。
以下是如何修改您的代码来实现判别器:
const mongoose = require("mongoose");
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
role: {
type: String,
default: "user",
},
});
const User = mongoose.model("User", userSchema);
const doctorSchema = new mongoose.Schema({
fees: {
type: Number,
default: 1000,
},
specialization: {
type: String,
default: "Physicist",
},
location: {
type: String,
default: "India",
},
});
const Doctor = User.discriminator("Doctor", doctorSchema);
module.exports = { User, Doctor };
在上面的代码中,我们定义了基本的用户模式,包括共同的字段如姓名、电子邮件、密码和角色。然后,我们创建了一个名为doctorSchema
的新模式,其中包括特定于医生角色的附加字段。
使用User.discriminator("Doctor", doctorSchema)
,我们创建了一个名为Doctor的判别器模型,该模型基于User模型。这将允许我们在角色属性设置为"doctor"时创建Doctor模式的实例。
现在,在注册用户时,您可以检查角色值并相应地创建用户:
const { User, Doctor } = require("./userSchema");
// 注册用户时的示例用法
const registerUser = async (userData) => {
let newUser;
if (userData.role === "doctor") {
newUser = new Doctor(userData);
} else {
newUser = new User(userData);
}
// 不要忘记为此添加错误处理。
await newUser.save();
}
注意:以上是代码的翻译部分,没有其他内容。
英文:
To achieve conditional schema fields based on the role property in the user schema, you can make use of Mongoose's discriminators.
Discriminators allow you to define different schemas based on a common parent schema.
Here's how you can modify your code to implement discriminators:
const mongoose = require("mongoose");
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
role: {
type: String,
default: "user",
},
});
const User = mongoose.model("User", userSchema);
const doctorSchema = new mongoose.Schema({
fees: {
type: Number,
default: 1000,
},
specialization: {
type: String,
default: "Physicist",
},
location: {
type: String,
default: "India",
},
});
const Doctor = User.discriminator("Doctor", doctorSchema);
module.exports = { User, Doctor };
In the above code, we define the base User schema with common fields like name, email, password, and role. Then, we create a new schema called doctorSchema that includes the additional fields specific to the doctor role.
Using User.discriminator("Doctor", doctorSchema)
, we create a discriminator model called Doctor based on the User model. This will allow us to create instances of the Doctor schema when the role property is set to "doctor".
Now, when registering a user, you can check the role value and create a user accordingly:
const { User, Doctor } = require("./userSchema");
// Example usage when registering a user
const registerUser = async (userData) => {
let newUser;
if (userData.role === "doctor") {
newUser = new Doctor(userData);
} else {
newUser = new User(userData);
}
// don't forget to add error handling for this.
await newUser.save()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论