0
我想要使用foreach循環來迭代MongoCursor,如下面的代碼所示。循環只返回第一條記錄。任何人都可以弄清楚是什麼問題?使用foreach循環迭代MongoCursor問題使用PHP
這裏是我的代碼
$usrcollection = $db->users;
$user = new MongoId($userid);
$where = array('_id' => $user);
$filter = array('courses.coursename' =>true, 'courses.startdate' => true,'courses.duration' => true, 'courses.status' => true,'courses.completed' => true,'courses.institute' => true,'courses.instructor' => true,'courses.coursecreated' => true, '_id' => 0);
$usercursor = $usrcollection->find($where,$filter);
$j = 0;
foreach ($usercursor as $value) {
echo($value['courses'][$j]['coursename']);
$j++;
}
的print_r的輸出是
Array ([courses] => Array ([0] => Array ([coursename] => Machine learning [startdate] => 2013-12-01 [duration] => 12 Weeks [institute] => Standford University [instructor] => Dr Lafore [status] => Inprogress [completed] => 20 [coursecreated] => MongoDate Object ([sec] => 1386751059 [usec] => 0)) [1] => Array ([coursename] => Fashion Design [startdate] => 2013-12-10 [duration] => 3 Weeks [institute] => MIT [instructor] => Ebay [status] => Inprogress [completed] => 0 [coursecreated] => MongoDate Object ([sec] => 1386753755 [usec] => 0))))
MongoDB的架構是低於
{
"_id": ObjectId("52972705f770dff815000002"),
"badge": "Gold",
"badgecount": NumberInt(3),
"badgedate": ISODate("2013-11-13T05:53:49.0Z"),
"badges": {
"0": {
"badgename": "Silver",
"achievedon": ISODate("2013-10-05T05:53:49.0Z")
},
"1": {
"badgename": "Gold",
"achievedon": ISODate("2013-11-13T05:53:49.0Z")
}
},
"courses": {
"0": {
"coursename": "Machine learning",
"startdate": "2013-12-01",
"duration": "12 Weeks",
"institute": "Standford University",
"instructor": "Dr Lafore ",
"status": "Inprogress",
"completed": "20",
"coursecreated": ISODate("2013-12-11T08:37:39.0Z")
},
"1": {
"coursename": "Fashion Design",
"startdate": "2013-12-10",
"duration": "3 Weeks",
"institute": "MIT",
"instructor": "Ebay",
"status": "Inprogress",
"completed": NumberInt(0),
"coursecreated": ISODate("2013-12-11T09:22:35.0Z")
}
},
"datejoined": ISODate("2013-10-01T05:53:49.0Z"),
"education": {
"0": "Msc Computer science"
}
}
謝謝!它的工作完美。 –
好吧,檢查你的問題,因爲答案,所以脂肪酶知道它的回答:) – gprusiiski