为什么我从MongoCollection :: find获取一个空数组

时间:2015-01-05 14:02:41

标签: php mongodb

当我使用以下PHP代码时:

$m = new MongoClient();
$db = $m->selectDB('mylocalmap_development');
$collection = new MongoCollection($db, 'stores');
$cursor = $collection->find();
var_dump($cursor); exit;

..我得到一个空数组:

object(MongoCursor)#82 (0) { } 

但是,如果我执行以下操作,我可以看到该集合中有一条记录:

$ mongo
MongoDB shell version: 2.4.12
connecting to: test
> use mylocalmap_development
switched to db mylocalmap_development
> db.stores.find();
{ "_id" : ObjectId("54aa9626adc9f013088b4567"), "name" : "The Greengrocer", "address" : "123 Fake Street", "city" : "Stirling" }

我做错了什么?据我所知,我正在按照文档进行操作

http://php.net/manual/en/mongocollection.find.php

1 个答案:

答案 0 :(得分:1)

尝试iterator_to_array($cursor)查看光标的内容。如果有记录,则该数组不会为空:

var_dump(iterator_to_array($cursor));