Updating database 1 based on results in database 2

For my tutoring site, I have a database that contains the subjects offered and another database with my tutors. Using cron, I want to run a function that updates the number of tutors I have in each subject in my “subjects” database. The number of tutors in each subject must be determined by counting the number of tutors that do a subject in the “tutors” database.

The subject names are in the “value” field of the “subjects” database. These same subject names are then fields themselves in the “tutors” database of type boolean. In the tutors database, each row is a different tutor with the subjects they tutor in “ticked” in the respective column for that subject.

What is the most efficient way of counting the number of tutors for each subject?

I tried doing it with a for loop but got the error “WebMethod request timed-out after 14 seconds… Did you forget to resolve a promise?”.

export async function updateTutorCount ( curriculum ) {

**const**  subjRes  =  **await**  wixData . query ( "subjects" ). find () 
**const**  currSubjects  =  subjRes . items  // an array of objects 
**const**  updatedSubjects  = [] 

**for**  ( **let**  subjectObj  **of**  currSubjects ) { 
    **const**  tutorRes  =  **await**  wixData . query ( "tutors" ). eq ( subjectObj . value , **true** ). find () 
    subjectObj . tutorCount  =  tutorRes . items . length 
    updatedSubjects . push ( subjectObj ) 

} 

console . log ( updatedSubjects ) 

}

Any help is much appreciated :grinning:! Thanks !