2016-11-23 44 views
0

我是ExpressJs和NodeJS的新手。我正试图将我的userInformation获取到Appointment模式。我如何从约会架构访问我的用户架构?我敢肯定,我们将不得不使用填充,但我不知道如何使用它从预约架构中获取用户信息

预约模式

var appointmentSchema = mongoose.Schema({ 
    userId: String, 
    visitType : String, 
    timeDuration : String, 
    startTime : Date, 
    endTime : Date, 
    status : String 

    }); 

用户模式:

var userSchema = mongoose.Schema({ 
    _id : String, 
    prefix : String, 
    firstName : String, 
    middleName : String, 
    lastName : String, 
    dob : String, 
    age : Number, 
    gender : String 
}); 

在此先感谢

回答

0

那么,我会假设那些TWO以上模式的名字是AppointmentUser r espectively,到目前为止还没有涉及彼此的模式,所以:

第一件事:你需要一提的是userId属性在Appointment模式是指User架构如下:

userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }, 
而不是

userId: String, 

第二件事:在尝试获取来自Appointment schema你userInformation,您的查询将是:

Appointment 
    .find({ /* whatever your filters options */ }) 
    .populate('userId') 
    .exec() 

了解更多关于猫鼬人口http://mongoosejs.com/docs/populate.html

+0

不应该,我们必须添加_id:{类型:mongoose.Schema.Types.ObjectId,ref:'Appoinment'}以及用户架构。 @Basim Hennawi – Vignesh

+0

'User'和'Appointment'之间的关系是什么? 1:1? 1:N?用户模式中的'_id'属性是指什么? userId或appointmentId? –

+0

1:N和用户的_id是约会架构中的userId。 – Vignesh