I am a newbie to ExpressJs and NodeJS . I am trying to fetch my userInformation to Appointment schema. How would I access my user schema from appointment schema? I am quite sure that we will have to use populate, But I am not aware of how to use it
Appointment Schema
var appointmentSchema = mongoose.Schema({
userId: String,
visitType : String,
timeDuration : String,
startTime : Date,
endTime : Date,
status : String
});
User Schema :
var userSchema = mongoose.Schema({
_id : String,
prefix : String,
firstName : String,
middleName : String,
lastName : String,
dob : String,
age : Number,
gender : String
});
Thanks in advance
Well, I'll assume that the names of those TWO above schemas are Appointment
and User
respectively, so far there is nothing relates those schemas with each other, so:
First thing: You need to mention that userId
attribute in Appointment
schema refers to User
schema as follow:
userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
instead of:
userId: String,
Second thing: On trying to fetch your userInformation from Appointment schema
, your query will be:
Appointment
.find({ /* whatever your filters options */ })
.populate('userId')
.exec()
Read more about mongoose population: http://mongoosejs.com/docs/populate.html