我知道 python-eve 支持 aggregation和 filtering 。我知道如何单独使用它们:
$ 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)])}
]
}
}
}
我可以使用如下查询 URL:
http://eve-demo.herokuapp.com/people?where={"lastname": "Doe"}&aggregate={"$value": 2}
最佳答案
简短的回答是是,但您需要使用$match
命令在聚合中定义过滤。 data_source
中的 filter
键不接受来自 url 的参数。
例如,
推荐的东西= {
'url': '事物/推荐/',
'数据源': {
'来源':'事物',
'聚合':{
'管道':[
{"$match": {"id":"$id"}},
{“$查找”:{
“来自”:“other_collection”,
"localField": "localField",
"foreignField": "foreignField",
“as”:“some_field”}}
]
}
}
}
查询网址就像
some_url/东西/推荐?aggregate={"$id": 1}
请注意,您需要使用 encoderUrlComponent
和 JSON.stringfy
来转义此 url 中的字符。
您甚至可以通过整个匹配标准:
推荐的东西= {
'url': '事物/推荐/',
'数据源': {
'来源':'事物',
'聚合':{
'管道':[
{"$match": "$where$},
{“$查找”:{
“来自”:“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
来转义此 url 中的字符。
我在我的电脑上测试过,它可以工作。
关于python - 我可以在 python-eve 中指定位置并同时聚合吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45513787/