2012-04-15 25 views
0

根据答案更新了问题。看到原来的问题向下滚动...如何使用Mongodb中另一个查询的结果集编写复杂查询?

所以,这是我结束了与stuColl.find调用之后JSON:

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

现在我想用上面的方法来做另一个查找来获得所有教授ID。但我得到的是一个空结果。我觉得我非常接近。我做错了什么:

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); 
       }); 
      }); 
     }); 

任何帮助非常感谢!


我写使用蒙戈(蒙戈使用本机驱动程序),Node.js的和快速的应用程序。

我有mongo的学生,课程和教授文档。

我想检索所有'教授'文档的列表,这些文档的学生目前正在服用或已服用过的课程。

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

这就是我打算做:1。 我第一个问题的查询检索所有学生课程的ID。 2.然后,我必须撰写并发出另一个查询来获得所有这些课程的教授ID。 3.最后,我必须得到与教授id相关的所有“教授”文件。

第1步不是问题,现在我拥有所有的课程ID。但我不知道如何做第二步。第二步和第三步是相似的,一旦我找出第二步,第三步将会很容易。

基本上我想在步骤2中发出一个查询来检索所有教授ID。我不想为10个课程ID发出10个单独的查询。

这里是我有:

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

回答

0

我认为不是$或你应该使用$在操作

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

stuColl.find.... 

应将返回结果toArray用于$ in操作符中。

您好在抢位的:-) 但请检查如果这是你在找什么:

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); 
      }); 
     });   
    } 
}); 

它进入低谷学生收集和发现的学生,然后通过他的一切/她的课程,以最终加载所有的老师。是吗?

+0

coffeeyesplease, 感谢您的回应。 从代码中可以看到,stuColl.find将返回顶级courseid,并且还可能包含嵌入数组的课程id。 $ $能够看到两个层面吗?或者将toArray能够将多级结果扁平化为1级对象ID数组?如果不是,这将是一个好办法吗? SU – 2012-04-16 00:33:25

+0

要回答你的问题,没有mongo不会把它弄平。如果这真的是你想要的,那么mapReduce函数可能会对你有所帮助。让我知道这是否有帮助。 – coffeeyesplease 2012-04-16 16:06:25

+0

coffeeyesplease ....就是这样。非常感谢。 – 2012-04-17 13:33:32

相关问题