MongoDB分页-使用限制和跳过时的结果总数?(过滤器阵列)



我需要知道我的搜索查询中包含的用于创建分页的行/文档的总数。

这是代码:


$filter= ['$text' => ['$search' => $code] ,'countries_tags' => 'en:' . strtolower($langs_list[$this->page->lang][3])];
$options = array('limit'=>$limit, 'skip'=>$skip,'sort'=>['images'=>-1]); //'sort'=>['images'=>-1]
$query = new MongoDBDriverQuery($filter, $options);
$cursor = $this->mongoDB->executeQuery('off.products', $query);
$this->results = array();
foreach ($cursor as $document) {
$this->results[] =  $document;
}

$this->rows_found = 300;// How can I get the correct number of hits??

$this->results[]只包含特定页面的行/文档总数,但搜索本身可能包含300、500甚至1000个结果,我想知道有多少实际结果。

有没有一个简单的参数我只需要扔到这个查询中就可以得到总结果的数量?

我几乎什么都试过了。但当我搜索PHP和MongoDB时,我发现的是这种过滤的方法调用,但我拥有的整个代码";"接管";使用数组发送参数。

$facet适用于以下情况:

db.collection.aggregate([
{
"$facet": {
"total": [
{
// your query
},
{
"$count": "pages"
}
],
"current_page": [
{
// your query
},
{
$skip: 1
},
{
$limit: 2
}
]
}
}
])

解释:

将您的查询发送到第1个方面,在那里您可以跳过&限制和相同的查询到具有{$count}阶段的2nd facet元素,以便在相同的聚合中也接收总文档。

游乐场

最新更新