致命错误:不能使用MongoCursor类型的对象作为数组

I'm using MongoDB with php - I'm trying to get the id of the user who has logged in with a username & password and store it as a session variable.

I keep getting this error and I don't understand why.

<?php
session_start();

$dbhost = 'localhost';
$dbname = 'story';

$mongo = new MongoClient("mongodb://$dbhost");
$db = $mongo->$dbname;

$collection = $db->users;

$email=$_POST['email'];
$password=$_POST['password'];

$user = $collection->find(array("email" => $email, "password" => $password));
$user->limit(1);
if ($user->count(true) > 0){
    $_SESSION['user']['userid']=iterator_to_array($user['_id']->{'$id'});
    header("Location:../webpages/master.php?page=home");
}else{
    echo "Wrong username or password";
}

?>

find() returns a cursor, on which you can iterate to retrieve results.

While in case of single document retrieval,use findOne()

$user = $collection->findOne(array("email" => $email, "password" => $password));

And you need not to use following line :

$user->limit(1); // Don't use limit() with findOne()

As Sammaye has indicated the find() function returns a cursor that you then iterate over to find documents. If you know there is only one document then rather than using find(...)->limit(1), which still returns a cursor, you can use findOne(), which returns the document itself.