Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue in iterating MongoCursor using foreach loop with php
    primarykey
    data
    text
    <p>I am trying to iterate the MongoCursor using the foreach loop as shown in the code below. The loop only returns the first record. Can anyone figure out what the issue is?</p> <p>Here is my code </p> <pre><code>$usrcollection = $db-&gt;users; $user = new MongoId($userid); $where = array('_id' =&gt; $user); $filter = array('courses.coursename' =&gt;true, 'courses.startdate' =&gt; true,'courses.duration' =&gt; true, 'courses.status' =&gt; true,'courses.completed' =&gt; true,'courses.institute' =&gt; true,'courses.instructor' =&gt; true,'courses.coursecreated' =&gt; true, '_id' =&gt; 0); $usercursor = $usrcollection-&gt;find($where,$filter); $j = 0; foreach ($usercursor as $value) { echo($value['courses'][$j]['coursename']); $j++; } </code></pre> <p>The output of print_r is </p> <pre><code>Array ( [courses] =&gt; Array ( [0] =&gt; Array ( [coursename] =&gt; Machine learning [startdate] =&gt; 2013-12-01 [duration] =&gt; 12 Weeks [institute] =&gt; Standford University [instructor] =&gt; Dr Lafore [status] =&gt; Inprogress [completed] =&gt; 20 [coursecreated] =&gt; MongoDate Object ( [sec] =&gt; 1386751059 [usec] =&gt; 0 ) ) [1] =&gt; Array ( [coursename] =&gt; Fashion Design [startdate] =&gt; 2013-12-10 [duration] =&gt; 3 Weeks [institute] =&gt; MIT [instructor] =&gt; Ebay [status] =&gt; Inprogress [completed] =&gt; 0 [coursecreated] =&gt; MongoDate Object ( [sec] =&gt; 1386753755 [usec] =&gt; 0 ) ) ) ) </code></pre> <p>The MongoDb schema is below</p> <pre><code>{ "_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" } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload