DEV Community

Cover image for populate referencing fields
Kazi Abdur Rakib
Kazi Abdur Rakib

Posted on

populate referencing fields

//=> student.service.ts
const getAllStudentsFromBD = async () => {
  const result = await Student.find()
    .populate('admissionSemester')
    .populate({
      path: 'academicDepartment',
      populate: {
        path: 'academicFaculty',
      },
    });
  return result;
};
Enter fullscreen mode Exit fullscreen mode

here i got the admissionSemester id then I input semester id inside populate, then I get full details of this admissionSemester id.

//=>src/app/modules/academicDepartment/academicDepartment.model.ts
const academicDepartmentSchema = new Schema<TAcademicDepartment>(
  {
    name: {
      type: String,
      required: true,
      unique: true,
    },
    academicFaculty: {
      type: Schema.Types.ObjectId,
      ref: 'AcademicFaculty',
    },
  },
  {
    timestamps: true,
  },
);
Enter fullscreen mode Exit fullscreen mode

ref: 'AcademicFaculty',
.populate('modelname')

Top comments (0)