1

I am getting an array length of 1 even if my MongoDB collection is blank. I am using PHP and MongoDB.

code :

$listdata=$db->kf_poll_master->find();

 echo (count($listdata));exit;

Here my collection: kf_poll_master has no data still the array length is showing 1 in echo where it should come as 0.

2
  • Try var_dump($listdata) it is like nuclear bomb in PHP. Commented Aug 22, 2016 at 7:32
  • @RohanKumar : I did as per you but it gave me this object(MongoCursor)#7 (0) { } output. I also did like this print_r($listdata) and it gave me this MongoCursor Object ( ) as poupt. Commented Aug 22, 2016 at 7:56

1 Answer 1

3

MongoCollection::find() does not return a PHP array, but an object of class MongoCursor. You cannot use the native PHP count().

You should rather do :

echo $listdata->count();

Alternatively, you can convert the MongoCursor, which is an Iterator, to an array using iterator_to_array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.