2012-04-15 50 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

相關問題