遍历bson对象内部循环的问题

时间:2017-04-21 05:58:27

标签: jquery json mongodb bson

我在mongo db集合中有数据,如

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

                                                    }
                                                }
                                         }
                                     }
                                 }
                             }
                         }
                     });

请注意,我正在尝试将课程的所有学年添加为下拉列表项目。现在上面的代码只增加了两个学年 “2016年第二届Semister”和“2017年第二届Semister”。我想读取“2016年第二届Semister”,“2017年第2届Semister”,“Ist Semister 2016”和“Ist Semister 2017”学生的所有学年。

请帮助!!!

Mongo DB Query:

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

1 个答案:

答案 0 :(得分:1)

更新您的功能以使用distinct。这将针对给定的课程ID预测所有学年。

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