我可以在 python-eve 同时指定 where 和 aggregate 吗?

Can I specify where and aggregate simultaneously in python-eve?

我知道python-eve支持aggregation and 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}

简短的回答是 YES 但您需要使用 $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"}} ] } } }

查询 url 就像 some_url/things/recommended?aggregate={"$id": 1} 请注意,您需要使用 encoderUrlComponentJSON.stringfy 来转义此 url.

中的字符

你甚至可以通过整个匹配标准: things_recommended = { 'url': 'things/recommended/', 'datasource': { 'source': 'things', 'aggregation': { 'pipeline': [ {"$match": "$where$}, {"$lookup": { "from": "other_collection", "localField": "localField", "foreignField": "foreignField", "as": "some_field"}} ] } } }

查询 url 就像 some_url/things/recommended?aggregate={"$where": {"$or": [{"family": "$family_id"}, {"is_shared": True}]}} 请注意,您需要使用 encoderUrlComponentJSON.stringfy 来转义此 url.

中的字符

我已经在我的电脑上测试过,它可以工作。