MongooseJS将用户模式和配置文件模式分开



我正在尝试创建两个猫鼬模式,一个用于用户,主要用于身份验证,另一个用于用户名配置文件,其中包含用户的配置文件图像、生物…等

当使用mongoose钩子创建用户文档时,我如何自动创建配置文件文档,以及如何在两个模式之间创建(一对一(关系?

根据以下代码snip&根据您的要求更新参数

User.model.js

let Schema = mongoose.Schema;
let Profile = new Schema({
image: {
type: String,
required: true
},
bio: {
type: String,
required: true
}
});
let User = new Schema({
email: {
type: String,
required: true
},
password: {
type: String,
required: true
}
profile: Profile,
}, {timestamp: true});

module.exports = mongoose.model('User', User);

最新更新