如何使用 MongoDB PHP 扩展获取游标结果计数 [英] How to get cursor result count using the MongoDB PHP extension

查看:35
本文介绍了如何使用 MongoDB PHP 扩展获取游标结果计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mongo PHP 扩展在支持 mongodb 扩展.此扩展与 mongo-php-library.

在旧扩展中,可以使用 MongoCursor::count 从游标中获取结果计数().但是,新游标 MongoDB\Driver\Cursor 没有这样的方法.对MongoDB执行查询后获取结果数量的新方法是什么?

In the old extension one could get the result count from the cursor using MongoCursor::count(). However, the new cursor MongoDB\Driver\Cursor has no such method. What is the new way of getting the number of results from after performing a query against MongoDB?

推荐答案

我使用此代码.

$query = ["hello" => "world"];
$command = new MongoDB\Driver\Command(["count" => "collection", "query" => $query]);
try {
    $result = $mongo->executeCommand("mydb", $command);
    $res = current($result->toArray());
    $count = $res->n;
    echo $count;
} catch (MongoDB\Driver\Exception\Exception $e) {
    echo $e->getMessage(), "\n";
}

这篇关于如何使用 MongoDB PHP 扩展获取游标结果计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆