2 / 2
Mar 19

Hey everybody,

in short: Can I conditionally populate on the same level? (or something similar)

In long:
I have three databases:
(1) User
(2) Courses
(3) Lessons

User can be enrolled in Courses (Array of Objects with IDs in the User) and Courses consists of Lessons. (In the Courses db, each document has an array with lesson names in it)

Users should only get the information about the course lessons, after the course enrollment was confirmed by the admin. For this, the UserInterface has for each course enrollment a “confirmed” field as boolean:

interface UserInterface { name: string email: string password: string courseEnrollments?: { course: mongoose.Types.ObjectId confirmed?: boolean expiresOn?: Date }[] } interface CourseInterface { name: string lessons?: { name: string }[] }

My idea was to populate the User.courseEnrollments.course but only if the ````User.courseEnrollments.confirmed istrue `` .

When the enrollment is not confirmed, the server should not fetch any information about the lessons from the database.

I didn’t find out how to do it. Any ideas?

11 months later

According to your definition in UserInterface, a user can be enrolled in multiple courses. So I will do:

  1. Unwind courses field of User
  2. filter out courseEnrollments.confirmed == false
    Then I think it’s the result you want.