你正在嘗試做的的print_r在MongoCursor,而不是一個PHP數組(這是行不通的。)
http://php.net/manual/en/class.mongocursor.php
你需要任何轉換光標到一個PHP數組...
<?
// 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>";
}
?>
或者,使用findOne(),而不是返回一個MongoCursor ...所以如果你只是想得到一個文件和JSON返回到您的應用程序,你可以做到這一點很簡單,像這樣(這表明該怎麼辦JSON和print_r的,你問)...
請參閱以下文章更多的幫助...
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>";
?>
這個:https://gist.github.com/a1cceb8d4ec8ced1fbdb - 是我現在怎麼做...它似乎凌亂:\ – 2011-03-19 22:00:48