这是我第一次尝试将子架构的详细信息注册到父架构。我是Mongoose和Nodejs的后端新手。
我有一个父模式,我在一个数组中声明了一个子模式。首先,我正在注册父模式。现在,在注册之后,父模式的用户想要为智利模式创建另一个注册。我已经在同一模型文件中定义了两个模式。
父模式:-
var adminSchema = new mongoose.Schema({
companyName : {
type: String,
required: "Company name can't be empty.",
required: false
},
companyID: {
type: String,
},
admins: {
email : {
type: String,
required: "Email can't be empty.",
unique: true
},
password: {
type: String,
required: "Password name can't be empty."
},
firstName : {
type: String,
required: "First name can't be empty."
},
lastName : {
type: String,
required: "Last name can't be empty."
},
},
verified: String,
role: String,
users:[ userSchema ]
}
});
mongoose.model('Admin', adminSchema);
我要为其创建单独注册的子架构:-
var userSchema = new mongoose.Schema({
email : {
type: String,
required: "Email can't be empty.",
// unique: true
},
password: {
type: String,
required: "Password name can't be empty."
},
firstName : {
type: String,
required: "First name can't be empty."
},
lastName : {
type: String,
required: "Last name can't be empty."
},
verified: {
type: Boolean,
default: false
},
role: String,
});
那么,在哪里以及如何为userSchema创建另一个注册?我只有一个控制器来保存详细信息。
var admin = new Admin();
admin.companyName = req.body.companyName;
admin.admins = {
email : req.body.email,
password: req.body.password,
firstName : req.body.firstName,
lastName : req.body.lastName,
role : "admin",
verified :"false",
users: []
};
我还需要为子架构创建单独的模型和控制器文件吗?如果是,我如何将其与父模式链接并从文档中获取新数据的详细信息以作为响应?