如何使用mongodb在nestjs中创建嵌套模式


  • 电子邮件
  • 密码
  • 角色
  • 已确认
  • id
  • 地址
    • 城市
    • 街道

我的用户模式应该是这样的。我希望它显示为嵌套模式。分离地址空间。

export const UserSchema = new mongoose.Schema({
email: { type: String, unique: true, required: true },
password: { type: String, required: true },
role: { type: String, enum: Role, default: Role.USER },
confirmed: { type: Boolean, default: false },
id: { type: String, default: uuid.v4 },
});
UserSchema.pre('save', async function (next) {
try {
if (!this.isModified('password')) {
return next();
}
const hashed = await bcrypt.hash(this['password'], 10);
this['password'] = hashed;
return next();
} catch (err) {
return next(err);
}
});

界面用户

export interface User extends Document {
id:string;
email: string;
password: string;
role: Role[];
address: ?
}

我必须在用户中添加地址。怎样

export const AddrSchema= new mongoose.Schema({
city: { type: String, default: "bb" },
street: { type: String, default: "aa" },
})

我认为这会解决问题。

export const UserSchema = new mongoose.Schema({
email: { type: String, unique: true, required: true },
password: { type: String, required: true },
role: { type: String, enum: Role, default: Role.USER },
confirmed: { type: Boolean, default: false },
id: { type: String, default: uuid.v4 },
address: { 
city: { type: String, default: "bb" },
street: { type: String, default: "aa" },
}
});

最新更新