How to compose a complex query using resultset from another query in Mongodb?

Question updated based on the answer. To see original question scroll down...

So, this is the JSON I end up with after the stuColl.find call:

[{"current_course_id":"4f7fa4c37c06191111000005","past_courses":[{"course_id":"4f7fa4c37c06191111000003"},{"course_id":"4f7fa4c37c06191111000002"}]}]

Now I want to use the above to do another find to get all professor IDs. But All i get is a null result. I think I am very close. What am I doing wrong:

stuColl.find({_id : userId}, { 'current_course_id' : 1 , 'past_courses.course_id' : 1 , _id : 0 }).toArray(function(err, courseIdsArray)
        {
            db.collection('courses', function(err, courseColl)
            {
                courseColl.find({$or : [ {_id : 'courseIdsArray.current_courses_id' }, {_id : {$in : courseIdsArray.past_courses}} ]}, {'professor_ids' : 1}).toArray(function(err, professorIdsArray)
                {
                  res.send(professorIdsArray);
                });
            });
        });

Any help is much appreciated!


I am writing an application using mongo(using mongo native driver), node.js and express.

I have Students, Courses and Professors documents in mongo.

I want to retrieve a list of all 'Professor' documents whose courses a student is currently taking or has taken in the past.

Students: {courseid, ....}
Course: {professors, ....}
Professors: {....}

This is what I intend on doing: 1. I first issue a query to retrieve all the course ids for a student. 2. Then I have to compose and issue another query to get the professor id for all those courses. 3. And then finally I have to get all the "professor" documents associated with the professor ids.

Step 1 is not a problem and now I have all the course ids. But I am not sure how to do step2. Step2 and 3 are similar, once I figure out step 2, step3 will be easy.

Basically I want to issue one query in step2 to retrieve all the professor ids. I dont want to issue 10 separate queries for 10 course ids.

here is what I have:

function getProfsByStudent(req, res, next)
{
  db.collection('students', function(err, stuColl)
  {
    stuId = new ObjectID.createFromHexString(req.params.stuId);
    stuColl.find({_id : userId}, { 'current_course_id' : 1 , 'past_courses.course_id' : 1 , _id : 0 })
    {
      db.collection('courses', function(err, courseColl)
      {
        courseColl.find({$or : []}) // THIS IS WHERE I AM STUCK
      });
      res.send(posts);
    });
  });
}

SU

I think that instead of $or you should be using the $in operator

http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24in

and

stuColl.find....

should be returning it's result toArray to be used in the $in operator-.

Hi there in a bit of rush :-) but please check if this is what you're looking for:

db.collection("Students").find({_id : "8397c60d-bd7c-4f94-a0f9-f9db2f14e8ea"}, {CurrentCourses:1, PastCourses : 1, _id : 0}).toArray(function(err, allCourses){
    if(err){
        //error handling
    }
    else{           
        var courses = allCourses[0].CurrentCourses.concat(allCourses[0].PastCourses);           
        db.collection("Courses").find({ _id : { $in: courses}}, {"Professors" : 1, _id : 0}).toArray(function(err, professors){ 
            if(err){
                //error handling
            }
            var allProfs = [];
            for(var i = 0; i < professors.length; i++){
                allProfs = allProfs.concat(professors[i].Professors);
            }
            db.collection("Professors").find({ _id : { $in: allProfs }}).toArray(function(err, results){
                console.log(results);
            });
        });         
    }
});

it's goes trough the students collection and finds the student and then through all his/her courses to finally load all the teachers. Is that it?