Martyn
Martyn

Reputation: 6403

Why am I getting an empty array from MongoCollection::find

When I use the following PHP code:

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

..I get an empty array:

object(MongoCursor)#82 (0) { } 

But if I do the following, I can see that there is a record in that collection:

$ 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" }

What am I doing wrong? I'm following the documentation as far as I can see anyway

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

Upvotes: 1

Views: 108

Answers (1)

anhlc
anhlc

Reputation: 14469

Try iterator_to_array($cursor) to see the content of the cursor. If there is a record, the array will not be empty:

var_dump(iterator_to_array($cursor));

Upvotes: 1

Related Questions