2010-12-11 47 views
4

我使用PHP与Mongo对话,使用findOne($query)db.collection返回结果,如我所料。但将该命令更改为find()不会返回任何内容。

从shell中,如果使用db.collection.find(),它将返回所有文档。任何人都可以解释为什么PHP驱动程序的find()不会返回结果,即使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); 
     } 
    } 
+1

只是检查...你不是试图访问查找的结果作为一个对象是你吗?因为'find'返回并且array和'findOne'返回一个实体对象。 – rojoca 2010-12-11 03:44:57

+0

好问题。尝试两种方式,并没有结果的任何方式。 – buley 2010-12-11 04:14:17

+1

你能向我们展示你的PHP和查询在你的问题上面吗? – 2010-12-11 06:01:49

回答

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>'; 

?> 
+1

一个言语不多的人,但感谢您的澄清 - 解释OP的麻烦(以及我今天的挫败感)。 – snh 2015-03-10 14:26:05

相关问题