我可以指定python-eve中的哪个位置并同时进行聚合吗?
我知道python-eve支持aggregation和filtering。我知道如何分开使用它们:我可以指定python-eve中的哪个位置并同时进行聚合吗?
$ curl -i http://example.com/posts?aggregate={"$value": 2}
http://eve-demo.herokuapp.com/people?where={"lastname": "Doe"}
但问题是:我可以同时使用它们吗?
例如,我曾这样定义端点:
posts = {
'datasource': {
'aggregation': {
'pipeline': [
{"$unwind": "$tags"},
{"$group": {"_id": "$tags", "count": {"$sum": "$value"}}},
{"$sort": SON([("count", -1), ("_id", -1)])}
]
}
}
}
我可以使用查询网址,如:
http://eve-demo.herokuapp.com/people?where={"lastname": "Doe"}&aggregate={"$value": 2}
简短的回答是是,但你需要定义过滤与$match
命令汇总。 data_source
中的filter
键不接受来自url的参数。
例如, things_recommended = { 'url': 'things/recommended/', 'datasource': { 'source': 'things', 'aggregation': { 'pipeline': [ {"$match": {"id":"$id"}}, {"$lookup": { "from": "other_collection", "localField": "localField", "foreignField": "foreignField", "as": "some_field"}} ] } } }
查询网址就像是 some_url/things/recommended?aggregate={"$id": 1}
注意你需要使用encoderUrlComponent
和JSON.stringfy
逃脱字符这个网址。
你甚至可以通过整个比赛的标准: things_recommended = { 'url': 'things/recommended/', 'datasource': { 'source': 'things', 'aggregation': { 'pipeline': [ {"$match": "$where$}, {"$lookup": { "from": "other_collection", "localField": "localField", "foreignField": "foreignField", "as": "some_field"}} ] } } }
查询网址就像是 some_url/things/recommended?aggregate={"$where": {"$or": [{"family": "$family_id"}, {"is_shared": True}]}}
注意你需要使用encoderUrlComponent
和JSON.stringfy
逃脱字符这个网址。
我已经在我的电脑上测试过,它的工作原理。