How to request mongo in php?

So, I have this simple login function that tries to match the email address with the password in the database and compare it with the data entered by the user through the form.

function login($email, $password){
    $m = new Mongo("localhost"); 
    $m->connect();
    $db = $m->users;
    $collection = $db->test_collection;

    echo "<pre>";
    var_dump($collection->findOne(array('name' => 'john'))); //returns correctly
    var_dump($collection->find(array('name' => 'john'))); //returns mongo cursor object
    echo "</pre>";
}

I do not understand why find () returns a cursor object. The answers?

This is a mongo document

array(5) {
  ["_id"]=>
  object(MongoId)#22 (1) {
    ["$id"]=>"4d7eaa848baf84d32b000000"
  }
  ["activated"]=> (true)
  ["email"]=> "john@smith.com"
  ["name"]=> "john"
  ["password"]=> "334c4a4c42fdb79d7ebc3e73b517e6f8"
}

How would I make a β€œWHERE” query that could find both email and password in the same document? I obviously do not get paramates correctly for find () and findOne () queries. What is the correct syntax in PHP?

+5
source share
2 answers

find - : http://www.mongodb.org/display/DOCS/Queries+and+Cursors. , .

findOne - , , , , , . findOne , .

, :

var_dump($collection->findOne(array('name' => 'john', 'password' => '334c4a4c42fdb79d7ebc3e73b517e6f8')));

( , PHP - PHP )

+5

return count($collection->find(array('name' => $name, 'password' => $password)));
+1

All Articles