我正在使用PHP与findOne($query)
上的Mongo使用db.collection
进行对话,它会返回我期望的结果。但是将该命令更改为find()不会返回任何内容。
从shell中,如果使用db.collection.find(),则返回所有文档。任何人都可以解释为什么即使find()
在使用完全相同的查询时,PHP驱动程序的findOne()
也不会返回结果吗?
更新:这是代码。
find()
(不起作用):
$db = $connection->selectDB( $database );
$returned_collection = $db->selectCollection( $collection );
$cursor = $returned_collection->find( $query );
);
find()
调试输出:
query: array(1) {
["user_id"]=>
string(13) "4d03d13b71676"
}
1292099894 > mongo_wrapper.class.php > returned_collection: events.votes
db: object(MongoDB)#41 (2) {
["w"]=>
int(1)
["wtimeout"]=>
int(10000)
}
cursor: object(MongoCursor)#43 (0) {
}
findOne()
(有效):
$db = $connection->selectDB( $database );
$returned_collection = $db->selectCollection( $collection );
$cursor = $returned_collection->findOne( $query );
findOne()
调试输出:
query: array(1) {
["user_id"]=>
string(13) "4d03d13b71676"
}
1292099906 > mongo_wrapper.class.php > returned_collection: events.votes
db: object(MongoDB)#7862 (2) {
["w"]=>
int(1)
["wtimeout"]=>
int(10000)
}
cursor: array(7) {
["_id"]=>
object(MongoId)#7849 (1) {
["$id"]=>
string(24) "4d03d842d0645afaab4e92f6"
}
["user_id"]=>
string(13) "4d03d13b71676"
["timestamp"]=>
int(1292095809)
["context"]=>
string(3) "ms3"
["uri"]=>
string(120) "http://feeds.marketwatch.com/~r/marketwatch/podcasts/MarketwatchStupidInvestmentOfTheWeek/~3/3H-tMQLS9AA/siotw103009.mp3"
["type"]=>
string(8) "category"
["vote"]=>
int(-1)
}
两者都使用此调试代码:
if($debug->enabled) {
echo time() . " > mongo_wrapper.class.php > returned_collection: $returned_collection \n";
if($debug->dump) {
echo "db: ";
var_dump( $db );
echo "cursor: ";
var_dump( $cursor );
}
}
答案 0 :(得分:3)
<?php
$connection = new Mongo();
$db = $connection->database;
$collection = $db->collection;
echo '<pre>';
print_r($collection->findOne());
$cursor = $collection->find();
foreach ($cursor as $id => $value) {
echo "$id: ";
print_r($value);
}
echo '</pre>';
?>