MongoDB - Aggregation Framework (Total Count)

后端 未结 7 1925
余生分开走
余生分开走 2020-12-31 07:42

When running a normal \"find\" query on MongoDB I can get the total result count (regardless of limit) by running \"count\" on the returned cursor. So, even if I limit to re

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-31 08:36

    $facets aggregation operation can be used for Mongo versions >= 3.4. This allows to fork at a particular stage of a pipeline in multiple sub-pipelines allowing in this case to build one sub pipeline to count the number of documents and another one for sorting, skipping, limiting.

    This allows to avoid making same stages multiple times in multiple requests.

提交回复
热议问题