如何在php中查询mongo?

发布于 2024-10-22 13:06:14 字数 1016 浏览 6 评论 0原文

所以我有这个简单的登录功能,它尝试将电子邮件地址与数据库中的密码进行匹配,并将其与用户通过表单输入的数据进行比较。

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>";
}

我不明白为什么 find() 只返回一个游标对象。答案?

这是 mongo 文档

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

我将如何执行“WHERE”查询来在同一文档中找到电子邮件和密码?显然,我没有为 find() 和 findOne() 查询提供正确的参数。 PHP 中正确的语法是什么?

So I've got this simple login function that is trying to match email address with a password in the database and compare it with the user entered data via 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 don't understand why the find() only returns a cursor object. Answers?

This is the mongo document

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

How would I do a "WHERE" query that would find both email and password in the same document? I'm obviously not getting the paramaters correct for the find() and findOne() queries. What is the correct syntax in PHP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

脱离于你 2024-10-29 13:06:14

find 应该返回一个光标 - 您可以在这里阅读有关它们的更多信息:http://www.mongodb.org/display/DOCS/Queries+and+Cursors
要从光标获取文档,您必须对其进行迭代。

就您而言, findOne 可能就是您所追求的,因为您不希望多个用户具有相同的用户名和密码。 findOne 函数始终最多返回一个文档,因此不需要游标。

要在查询中包含多个谓词,只需向查询数组中添加更多字段即可:(

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

如果这是正确的 PHP - 我的 PHP 有点生疏)

find is supposed to return a cursor - you can read more about them here: http://www.mongodb.org/display/DOCS/Queries+and+Cursors .
To get the documents from the cursor, you will have to iterate over it.

In your case, findOne is probably what you are after since you don't expect multiple users with the same username and password. The findOne function always returns at most one document, so there is no need for a cursor.

To have multiple predicates in your queries, just add more fields to your query array:

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

(If that is correct PHP - my PHP is a bit rusty)

软糖 2024-10-29 13:06:14

尝试

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

try

return count($collection->find(array('name' => $name, 'password' => $password)));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文