Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add _meta to aggregation results (via $facet support) #1208

Wytamma opened this issue Nov 11, 2018 · 1 comment

Add _meta to aggregation results (via $facet support) #1208

Wytamma opened this issue Nov 11, 2018 · 1 comment


Copy link

Wytamma commented Nov 11, 2018

I think _perform_aggregation could be modified with $facet to return a count value with the aggregation results. I know limitaions says that it's not the intended behaviour, however, I think it would be beneficial.

Here's how I would consider implementing:

def _perform_aggregation(resource, pipeline, options):
    paginated_results = []
    if req.max_results > 1:
        limit = {"$limit": req.max_results}
        skip = {"$skip": ( - 1) * req.max_results}
        # sub-pipeline in $facet stage cannot be empty
        skip = {"$skip": 0}
    facet_pipelines = {}
    facet_pipelines["paginated_results"] = paginated_results
    facet_pipelines["total_count"] = [{"$count": "count"}]

    facet = {"$facet":facet_pipelines}


    getattr(app, "before_aggregation")(resource, req_pipeline)

    cursor =, req_pipeline, options).next()

    for document in cursor['paginated_results']:

    getattr(app, "after_aggregation")(resource, documents)

    response[config.ITEMS] = documents

    # add pagination info
    if config.DOMAIN[resource]["pagination"]:
        count = cursor['total_count'][0]['count']
        response[config.META] = _meta_links(req, count)

    return response, None, None, 200, []

This modification would change the aggregate pipeline considerably, so maybe a config variable could be used to control this behaviour for those already hooking into "before_aggregation".

Am I missing anything obvious? Should I try implementing this and submitting a PR?

Copy link


First, sorry for the late answer. This looks interesting, feel free to submit a PR and I will be happy to review it.

@nicolaiarocci nicolaiarocci added this to the 0.9 milestone Mar 28, 2019
@nicolaiarocci nicolaiarocci changed the title Add _meta to aggregation results Add _meta to aggregation results (via $facet support) Mar 28, 2019
@nicolaiarocci nicolaiarocci modified the milestones: 0.9, 0.8.2 Apr 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

No branches or pull requests

2 participants