You are trying to do print_r on MongoCursor, and not in a PHP array (which will not work.)
http://php.net/manual/en/class.mongocursor.php
You need to either convert the cursor to a PHP array ...
<? // Connect to Mongo and set DB and Collection $mongo = new Mongo(); $db = $mongo->twitter; $collection = $db->tweets; // Return a cursor of tweets from MongoDB $cursor = $collection->find(); // Convert cursor to an array $array = iterator_to_array($cursor); // Loop and print out tweets ... foreach ($array as $value) { echo "<p>" . $value[text]; echo " @ <b><i>" . $value[created_at] . "</i></b>"; } ?>
Or, instead use findOne (), which will not return MongoCursor ... so if you just want to get one document and return it as JSON in your application, you can do it quite simply like that (it shows how to do JSON and print_r, as you requested) ...
See these articles for more details.
http://learnmongo.com/posts/mongodb-php-install-and-connect/
http://learnmongo.com/posts/mongodb-php-twitter-part-1/
<?php $connection = new Mongo(); $db = $connection->test; $collection = $db->phptest; $obj = $collection->findOne(); echo "<h1>Hello " . $obj["hello"] . "!</h1>"; echo "<h2>Show result as an array:</h2>"; echo "<pre>"; print_r($obj); echo "</pre>"; echo "<h2>Show result as JSON:</h2>"; echo "<pre>"; echo json_encode($obj); echo "</pre>"; ?>
Justin jenkins
source share