2017-04-21 126 views
-1

我在蒙戈DB收集數據,如問題在內部循環遍歷BSON對象

  array (
     '_id' => new MongoId("58db5ef91d7859bc10000029"), 
     'course_id' => 11, 
     'coursename' => 'B.Tech Computer Science', 
     'coursecode' => 'BTCS', 
     'coursepattern' => '8 Semisters', 
     'coursedescription' => '4 years course', 
     'gradingsystem' => 'normal', 
     'is_deleted' => new MongoInt32(0), 
     'batch' => 
     array (
      0 => 
       array (
       'batch_id' => 71, 
       'batchcode' => 'BTEC1', 
       'batchname' => 'Semister Ist', 
       'status' => 'active', 
         'total_students' => 
           array (
           0 => 
          array (
          'academic_year' => 'Ist Semister 2016', 
          'start_date' => '12/12/2016', 
          'end_date' => '12/12/2017', 
          'total_students' => new MongoInt32(1), 
          ), 
           1 => 
          array (
          'academic_year' => 'Ist Semister 2017', 
          'start_date' => '12/12/2017', 
          'end_date' => '12/12/2018', 
          'total_students' => new MongoInt32(1), 
          ), 
         ), 
        ), 
        1 => 
          array (
           'batch_id' => 72, 
           'batchcode' => 'BTEC2', 
           'batchname' => 'Semister 2nd', 
           'status' => 'active', 
             'total_students' => 
                array (
                 0 => 
                 array (
                 'academic_year' => '2nd Semister 2016', 
                 'start_date' => '12/12/2016', 
                 'end_date' => '12/12/2017', 
                 'total_students' => new MongoInt32(1), 
                 ), 
                 1 => 
                 array (
                 'academic_year' => 
                 '2nd Semiter 2017', 
                 'start_date' => '12/12/2017', 
                 'end_date' => '12/12/2018', 
                 'total_students' => new MongoInt32(1), 
           ), 
          ), 
         ), 
         ), 
        ) 

我已經寫了jQuery的AJAX功能像

    $.ajax({ 
         url: 'index.php?action=fetchAcademicYearOnCourse', 
         type: 'POST', 
         dataType: 'JSON', 
         data:{course_id:course_id}, 
         success: function(data){ 
          console.log(data); 
          if (data.length) { 

           for(var i in data){ 
            var batch = data[i]['batch']; 
            if(batch.length) 
            { 
             for (var j in batch) 
             { 
               var total_students = batch[j]['total_students']; 
               if(total_students.length) 
               { 
                $('#session').html('<option value="0">Select</option>'); 
                for (var k in total_students) 
                { 
                 //append the session in the academic year drop down 
                 var academic_year = total_students[k]['academic_year']; 
                 $('#session').append($('<option>',{value: academic_year,text: ''+academic_year+''})); 

                } 
               } 
             } 
            } 
           } 
          } 
         } 
        }); 

注意,我想添加的所有學術年作爲列表項的下拉課程。現在,上面的代碼僅添加了兩個學年,即 「2nd Semister 2016」和「2nd Semister 2017」。我希望獲得「2016年第二季度」,「第二季度2017年」,「Ist Semister 2016」和「Ist Semister 2017」的學生的所有學年。

請幫忙!!!

蒙戈DB查詢:

   public function fetchAcademicYearOnCourse() 
{ 
    $this->collection = $this->db->studentTbl; 

    $this->collection = $this->db->courseTbl; 

    $query = array('_id' => new MongoId($this->course)); 

    $cursor = $this->collection->find($query); 

    return $cursor; 
} 
+0

請問你蒙戈DB查詢樣子? – Veeram

+0

我檢查了控制檯,發現它提取所有數據,但沒有在下拉列表中。 – Nida

+0

我同意,但您可以改善查詢以獲取僅學年,以便您可以直接在下拉列表中顯示。 – Veeram

回答

1

更新您的功能使用不同的。這將針對給定課程編號投影所有學年。

public function fetchAcademicYearOnCourse() 
{ 
    $this->collection = $this->db->studentTbl; 

    $this->collection = $this->db->courseTbl; 

    $query = array('_id' => new MongoId($this->course)); 

    $retval = $this->collection->distinct("batch.total_students.academic_year", $query); 

    return $retval; 
} 

這裏更多http://php.net/manual/en/mongocollection.distinct.php

+0

很好的答案......謝謝 – Nida