node.js - 如何在一个命令中使用 mongoose 检索配置文件和用户模式?
问题描述
如何将用户和个人资料放在一起?
这是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 },
});
推荐阅读
- javascript - 是否可以拦截 Vue Router 推送等动作?
- sql - Django ORM 查询
- firebase - 无法删除文档
- c# - 为什么这个数据库更新只在一台客户端电脑上什么都不做,而在其他几台电脑上却能正常工作?
- javascript - 如何在 Javascript 中对 Date.prototype.toLocaleString() 数组进行排序?
- flutter - 在 ListTile 中添加 3 个文本,更改每列的背景颜色,并在此列表中添加搜索
- python - 设置 QChartView 的背景颜色
- azure - Azure 存储:每个订阅是否可以拥有超过 250 个存储帐户?
- swift - Swift 5 Alamofire 5 - 解析 XML
- ios - Flurry 发送测试推送消息停止工作