When using Pyeve is it possible to pass parameters to an aggregation pipeline when using get_internal?

169 views Asked by At

I am trying to call an aggregation pipeline with parameters as part of my server code within eve.

The documentation and the code on the [github]https://github.com/pyeve/eve/blob/master/eve/methods/get.py#L122 suggests that I should be able to call the pipeline using get_internal and it should run with the parameters passed to it.

I have been trying

get_internal("pipeline", **{'_id': id, 'time': time})

but it appears that the _id and time parameters are not getting passed to the aggregate query.

I have verified that the pipeline is working by visiting the pipeline URL

<baseurl>?aggregate={"_id":"5fa904807d3037e78023a5192,"time":1604827480260}

but I would prefer to call it from within the server side code, rather than making a request if possible.

Is there something obvious that I am doing wrong here.

Thanks

1

There are 1 answers

0
ehu On

Unfortunately, you can not use parameters with get_internal and aggregation. _perform_aggregation only uses the immutable request object while _perform_find merges where from the request object with your lookup parameters with an $and.

You could do a http request to the url like you show works or you could use app.data.driver and manually perform the aggregation query by importing the pipeline and modifying it manually:

from Flask import current_app as app
from domain.yourcollection import my_eve_aggregation #  Import the aggregation definition

# Get the aggregation pipeline
pipeline = my_eve_aggregation['datasource']['aggregation']['pipeline']

# Replace parameters - you need to manually replace the correct stages
pipeline[0]['$match'] = {'_id':'5fa904807d3037e78023a5192','time':1604827480260}

# Get the mongo collection
datasource = my_eve_aggregation['datasource']['source']

# Set db collection
col = app.data.driver.db[datasource]

result = list(col.aggregate(pipeline))

And please do create an issue at https://github.com/pyeve/eve/issues for this missing feature