I am converting from mysqli to mongodb and having some problems with basic tasks.
This is part of a login authentication process, if the email and password matches to a record set in the dB i would like to get the _id and create a session variable.
Heres what i have so far, all the searches i’ve done seems to have info on getting the _id from different steps and i couldn’t modify it for my situation.
Also, if my logic is too literal of a translation from mysqli, please i’m open for suggestions on best practices for noSql
$collection = $db->members;
$query = array("email" => $email, "password" => $newpass);
//$cursor = $collection->findOne($query); // was told this is better but need deeper understanding
$cursor = $collection->find($query);
$result = $cursor->count();
if($result==1){
$_SESSION['email'] = $email;
//having problems here
$id = $cursor->findOne(array("_id" => $query['_id']));
$_SESSION['id'] = $id;
return true;
}else
return false;
QUESTION how do i get the _id after authenticating a unique username and password?
You should not run
findOneon the cursor. The cursor points to the result from the previous query, you can use iterator_to_array to get an array with all the found records. Or you can use findOne() in the first place – it returns the actual row