Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php mongodb聚合中的$limit是否作为可选参数?_Php_Mongodb_Aggregation Framework_Mongodb Php - Fatal编程技术网

Php mongodb聚合中的$limit是否作为可选参数?

Php mongodb聚合中的$limit是否作为可选参数?,php,mongodb,aggregation-framework,mongodb-php,Php,Mongodb,Aggregation Framework,Mongodb Php,我有一个api,其中有一个名为limit的可选参数,它接受一个整数并限制get请求中从api返回的文档数 在我的PHP应用程序中,当它是一个必需的参数时,实现这个限制是可以的,但是当它没有被指定为get请求的一部分时,处理它的最佳方法是什么 例如,是否有一种方法可以定义$limit并将其设置为所有文档?(在伪代码中,$limit=none) 如上所示,当需要limit参数时,它会根据需要运行。现在当它被指定时,我怎么能保留上面的代码而不指定限制呢?简短回答否 但是,您可以使用这样的if/else

我有一个api,其中有一个名为limit的可选参数,它接受一个整数并限制get请求中从api返回的文档数

在我的PHP应用程序中,当它是一个必需的参数时,实现这个限制是可以的,但是当它没有被指定为get请求的一部分时,处理它的最佳方法是什么

例如,是否有一种方法可以定义$limit并将其设置为所有文档?(在伪代码中,$limit=none)

如上所示,当需要limit参数时,它会根据需要运行。现在当它被指定时,我怎么能保留上面的代码而不指定限制呢?

简短回答否

但是,您可以使用这样的if/else条件:

<?php

if(intval($this->limit) != 0 ) {
    $list = $collection->aggregate( array( 
        array('$match' => array( ... )),
        array('$project' => array ( ... ))
        array('$limit' => intval($this->limit)) 
    );

    ));
} else {
    $list = $collection->aggregate( array( 
        array('$match' => array( ... )),
        array('$project' => array ( ... )), 
    );
}
?>

您可以手动生成where子句

// Declare a where clause with your fixed conditions
$whereClause = array(array('$match' => array(...)), array('$project' => array(...)));

// Check if there's a limit
if($this->limit != 0)
    array_push($whereClause, array('$limit' => $this->limit));

// Finally, call with the where clause we've generated above
$list = $collection->aggregate($whereClause);

你能给我们看一些代码吗?保持相同的块在if-else条件下。虽然在不限制结果的情况下从数据库中获取数据不是一个好的解决方案,但正如您所知,我省略了很多代码,我认为有一种更有效的方法可以做到这一点?似乎MongoDB的开发人员可以为此添加一个“例外”字符?感谢您指出这一点,这非常清晰简洁,正是我想要的。很高兴它有帮助!:)
// Declare a where clause with your fixed conditions
$whereClause = array(array('$match' => array(...)), array('$project' => array(...)));

// Check if there's a limit
if($this->limit != 0)
    array_push($whereClause, array('$limit' => $this->limit));

// Finally, call with the where clause we've generated above
$list = $collection->aggregate($whereClause);