最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何在一个命令中使用猫鼬检索配置文件和用户架构?

IT培训 admin 3浏览 0评论

如何在一个命令中使用猫鼬检索配置文件和用户架构?

如何将用户和个人资料放在一起?

这是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: '[email protected]' })

我尝试填充,但没有用:

User.findOne({ email: '[email protected]' }).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 }, });

如何在一个命令中使用猫鼬检索配置文件和用户架构?

如何将用户和个人资料放在一起?

这是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: '[email protected]' })

我尝试填充,但没有用:

User.findOne({ email: '[email protected]' }).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 }, });

发布评论

评论列表 (0)

  1. 暂无评论