I’m making a query to a MongoDB and I only want the first object. I know I could use findOne, but I’m still confused where I’m going wrong.
This does not work:
if ($cursor->count() > 0) {
$image = $cursor->current();
// neither does this work
// $image = $cursor[0];
return $image;
} else {
return false;
}
//echo $image->filename;
// Throws error: Trying to access property of non-object image
This works though:
if ($cursor->count() > 0) {
$image = null;
foreach($cursor as $obj)
$image = $obj;
return $image;
} else {
return false;
}
How about this:
Bonus: quote from the Doc page