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

Closed
Wytamma opened this issue Nov 11, 2018 · 1 comment
Closed

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

Wytamma opened this issue Nov 11, 2018 · 1 comment
Milestone

Comments

@Wytamma
Copy link
Contributor

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": (req.page - 1) * req.max_results}
        paginated_results.append(skip)
        paginated_results.append(limit)
    else:
        # sub-pipeline in $facet stage cannot be empty
        skip = {"$skip": 0}
        paginated_results.append(skip)
    
    facet_pipelines = {}
    facet_pipelines["paginated_results"] = paginated_results
    facet_pipelines["total_count"] = [{"$count": "count"}]

    facet = {"$facet":facet_pipelines}

    req_pipeline.append(facet)

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

    cursor = app.data.aggregate(resource, req_pipeline, options).next()

    for document in cursor['paginated_results']:
        documents.append(document)

    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?

@nicolaiarocci
Copy link
Member

Hi,

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
Projects
None yet
Development

No branches or pull requests

2 participants