Node.js 猫鼬中的模式关联

Node.js 猫鼬中的模式关联,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我有两种型号: 以下是用户模型: const userSchema = new mongoose.Schema({ email: { type: String, unique: true, required: true }, password: { type: String, required: true }, passwordResetToken: String, passwordResetExpires: Date, facebook: String, twitte

我有两种型号:

以下是用户模型:

const userSchema = new mongoose.Schema({
  email: { type: String, unique: true, required: true },
  password: { type: String, required: true },
  passwordResetToken: String,
  passwordResetExpires: Date,

  facebook: String,
  twitter: String,
  tokens: Array,

  profile: {
    name: String,
    gender: String,
    location: String,
    website: String,
    picture: String
  }
}, { timestamps: true });
以下是复兴模式:

const reviveSchema = new mongoose.Schema({
  reviveShowName: {type: String, required: true},
  reviveTitle: {type: String, required: true},
  reviveCategory: {type: String, required: true},
  reviveGoal: {type: Number, required: true},
  revivePhoto: {type: String, required: true},
  reviveVideo: {type: String},
  reviveStory: {type: String},
  author: {
      id: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "User"
      },
      name: String
    }
}, { timestamps: true });

在REVISE模型中,我试图引用作者并获取作者的id,这很有效。。。如何从profiles->name…(配置文件->名称…)中获取名称。。。?显然
name:String
是错误的…

基于嵌套对象的
ref
type
值,Mongoose关系起作用。在本例中,您已将
author
id
属性关联到
User
模型

如果要使用用户信息填充
作者
,只需执行以下操作:

author: {     
  type: mongoose.Schema.Types.ObjectId,
  ref: "User"
}
然后在查询中只需使用“填充”

Revive.find({})
      .populate( 'author' )
      .exec( function( error, docs ) {
          console.log( docs ); // will have `[{author:{profile:{...}}}]` data
      } );          

Mongoose关系基于嵌套对象的
ref
type
值工作。在本例中,您已将
author
id
属性关联到
User
模型

如果要使用用户信息填充
作者
,只需执行以下操作:

author: {     
  type: mongoose.Schema.Types.ObjectId,
  ref: "User"
}
然后在查询中只需使用“填充”

Revive.find({})
      .populate( 'author' )
      .exec( function( error, docs ) {
          console.log( docs ); // will have `[{author:{profile:{...}}}]` data
      } );