如何同时拥有用户和个人资料?
架构的定义User
如下:
const UserSchema = new Schema({
email: { type: String, required: true },
name: { type: String, required: true },
password: { type: String },
picture: { type: String },
});
和配置文件架构:
const ProfileSchema = new Schema({
user: { type: Schema.Types.ObjectId, ref: 'User' },
setting1: { type: String, enum: ['YES', 'NO', 'UNKNOWN'], default: 'UNKNOWN' },
setting2: { type: String, enum: ['YES', 'NO', 'UNKNOWN'], default: 'UNKNOWN' },
setting3: { type: String, enum: ['YES', 'NO', 'UNKNOWN'], default: 'UNKNOWN' },
});
当我请求用户时,如何在一个命令/行中检索profile
?
User.findOne({ email: 'blabla@gmail.com' })
我尝试填充,但它不起作用:
User.findOne({ email: 'blabla@gmail.com' }).populate('profile')
- 请注意:我不想在同一架构中的用户中使用配置文件。
您需要在UserSchema中链接配置文件:
const UserSchema = new Schema({
profile: { type: Schema.Types.ObjectId, ref: 'Profile' },
email: { type: String, required: true },
name: { type: String, required: true },
password: { type: String },
picture: { type: String },
});