mongodb num_rows equivalent php


mongodb num_rows equivalent php

如何获得结果数,相当于mongodb中的num_rows(mysqli)

如果我有

$db->$dbName->find(array("email" => $newemail, "password" => $newpass));

检查符合此标准的结果数量的最佳方法是什么

在mysqli,我会做一些类似的事情

$sql = "SELECT id FROM table WHERE email='%s' AND password='%s'";
$query = sprintf($sql, $newemail, $newpass);
$result = $conn->query($query);
$rows = $result->num_rows;

像这样,我认为:

$collection = $connection->myDB->myCollection;
$cursor = $collection->find(array("email" => $newemail, "password" => $newpass));
$nResults = $cursor->count();

我相信您正在使用的返回一个MongoCursor,因此您可以使用它的方法count来获取行数。

请注意,count()是标准的标准,而不是skiplimit的标准。这与PHP中的MySQLi驱动程序略有不同,因为它考虑了LIMIT

为了了解真实计数,如果您使用的是跳过和限制,则需要将光标实际转换为数组,或者再次使用count()函数在其签名中提供参数:http://www.php.net/manual/en/mongocursor.count.php将其设置为true应该只会得到考虑sip和limit的实际结果。

否则,对于您的示例,正如其他人所说,您可以正常使用count。对于您提供的示例,我可能实际使用findOne