python - 我可以在 python-eve 中指定位置并同时聚合吗?

标签 python mongodb aggregation-framework where-clause eve

我知道 python-eve 支持 aggregationfiltering 。我知道如何单独使用它们:

$ 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} 请注意,您需要使用 encoderUrlComponentJSON.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}]}} 请注意,您需要使用 encoderUrlComponentJSON.stringfy 来转义此 url 中的字符。

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

关于python - 我可以在 python-eve 中指定位置并同时聚合吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45513787/

相关文章:

python - 值错误: Domain error in arguments scipy rv_continuous

xml - 使用 NLog 在 Mongo 数据库中存储日志

mongodb - 用 MongoDB 替换 PostgreSQL?

regex - 展开对象数组 mongoDB

Mongodb聚合$group、$sum和$sort

python - MapReduce 对多个文件中的所有行进行成对比较

python - 多态函数的功能注释

python - 索引错误: list index out of range when selecte values in openerp

mongodb - 我可以在没有 docker-compose 的情况下从其他 docker 容器播种 mongodb docker 容器吗?

mongodb - 在 mongoDB 聚合框架中组合组和项目