Mongodb 使用 PHP 变量的查询不适用于 Limit

Mongodb query with PHP variable is not working with Limit

我已经用 php 为 Mongodb 写了一个脚本,当 limit 是静态数字时它工作正常,而当我们在脚本中传递变量时它会抛出错误。

脚本代码为:

array(
                                "$match" => array(
                                    "$or" => array(
                                        array(
                                            'title' => new \MongoDB\BSON\Regex($queryString),
                                        ),
                                        array(
                                            'description' => new \MongoDB\BSON\Regex($queryString),
                                        ),
                                        array(
                                            'master.title' => new \MongoDB\BSON\Regex($queryString),
                                        ),
                                    ),

                                    "$and" => array(
                                    array(
                                        'created_on' => new \MongoDB\BSON\Regex($filterDate)
                                        )
                                    )
                                )
                            ),

                            array(
                                '$sort' => array(
                                    'created_on' => -1
                                )
                            ),
                            array(
                                '$limit' => $filterLimit,
                            )

'$limit' => $filterLimit,在我们传递静态数字时无法正常工作。

如果我们替换

                              array(
                                    '$limit' => $filterLimit,
                                )

                             array(
                                    '$limit' => 10,
                                )

它的工作fine.Below是错误

致命错误:未捕获MongoDB\Driver\Exception\CommandException:限制必须指定为

中的数字

$filterLimit 可能是字符串,尝试将其转换为整数:

array(
    '$limit' => intval($filterLimit),
)