javascript 聚合结果计数和分页

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 聚合结果计数和分页相关的知识,希望对你有一定的参考价值。

// https://stackoverflow.com/questions/20348093/mongodb-aggregation-how-to-get-total-records-count

// Pre 3.4
$result = $collection->aggregate(array(
  array('$match' => $document),
  array('$group' => array('_id' => '$book_id', 'date' => array('$max' => '$book_viewed'),  'views' => array('$sum' => 1))),
  array('$sort' => $sort),

// get total, AND preserve the results
  array('$group' => array('_id' => null, 'total' => array( '$sum' => 1 ), 'results' => array( '$push' => '$$ROOT' ) ),
// apply limit and offset
  array('$project' => array( 'total' => 1, 'results' => array( '$slice' => array( '$results', $skip, $length ) ) ) )
))

// 3.4+
$result = $collection->aggregate([
  { ...execute queries, group, sort... },
  { ...execute queries, group, sort... },
  { ...execute queries, group, sort... },
  $facet: {
    paginatedResults: [{ $skip: skipPage }, { $limit: perPage }],
    totalCount: [
      {
        $count: 'count'
      }
    ]
  }
]);

以上是关于javascript 聚合结果计数和分页的主要内容,如果未能解决你的问题,请参考以下文章

在 ElasticSearch 6 中按子聚合过滤、排序和分页

MySQL——分组查询和分页查询

MySQL——分组查询和分页查询

javascript 使用过滤器和分页获取所有元素

五种函数子查询和分页查询

跨微服务查询/分页