1
   //connects to mongo
   $collection  = (new MongoDB\Client)->mfrmls->properties;

   //takes array with data from array of objects            
   foreach ($obj as $k => $v){

    //prints to make sure on the right record.  this works fine
    echo "object {$v['ListingId']} \n";

    // creates a query to the mongo DB, which works fine, i have checked via var_dump
    $cursor = $collection->find(['ListingId'  => $v['ListingId']]);

    //this for each will never print anything it just seems to be skipped.
    //if i take it out of the nested foreach it works fine.
    foreach($cursor as $document) {
            echo "no no \n";
            }
    }

the results I get are :

object G4849756 
object A4202291 
object O5548422 
object O5548513 
object D5921405 

which is clearly missing the second for loop echo.

just for reference. "$cursor" only gets called once the foreach statement runs, i guess this is just how mongoDB - PHP works.

1
  • Try adding a var_dump($cursor); right after setting it to find out what's really in it. I'm also thinking maybe (I'd have to confirm) you may need to convert it from a obect to an array before you can use foreach on it. Not sure about that. Commented Mar 11, 2018 at 17:24

2 Answers 2

1

foreach only iterates over array. You can check what data type of $cursor by using var_dump($cursor);

Sign up to request clarification or add additional context in comments.

1 Comment

its a object, MongoDB\Driver\Cursor Object ( [database] => mfrmls [collection] => properties [query] => MongoDB\Driver\Query Object
1

Your code missing some checks. try this:

//connects to mongo
   $collection  = (new MongoDB\Client)->mfrmls->properties;

   //takes array with data from array of objects            
   foreach ($obj as $k => $v){

    //prints to make sure on the right record.  this works fine
    echo "object {$v['ListingId']} \n";

    // creates a query to the mongo DB, which works fine, i have checked via  
    $cursor = $collection->find(['ListingId'  => $v['ListingId']]);
print_r( $cursor);
    //this for each will never print anything it just seems to be skipped.
    //if i take it out of the nested foreach it works fine.
    if((is_array($cursor) || is_object($cursor)) && !empty( $cursor)) {
       foreach($cursor as $document) {
            echo "no no \n";
            }
     }
    }

3 Comments

MongoDB\Driver\Cursor Object ( [database] => mfrmls [collection] => properties [query] => MongoDB\Driver\Query Object ( [filter] => stdClass Object ( [ListingId] => D5921405 ) [options] => stdClass Object ( [serverId] => 1 ) [readConcern] => )
should have no issue with this type of object.
echo something in else condition and check, Is it coming in foreach or not ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.