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)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay