【问题标题】:Use variables in geonear aggregator in Python Eve with MongoDB在 Python Eve 和 MongoDB 中使用 geonear 聚合器中的变量
【发布时间】:2017-06-03 13:57:36
【问题描述】:

我正在尝试使用 Eve 通过 geoNear 聚合运算符查询 MongoDB 数据库,但在使用聚合变量时我得到以下信息:

'OperationFailure: geoNear command failed: { ok: 0.0, errmsg: "'near' field must be point", code: 17304, codeName: "Location17304" }'

聚合端点在settings.py中设置如下:

geoAggr = {'datasource':{
    'source':'geo',
    'aggregation': { 
        'pipeline': [
            {'$geoNear':{ 
                'near': {'type':'Point', 'coordinates':['$lon', '$lat']},
                'distanceField':'distance',
                'maxDistance':'$maxDistance',
                'spherical': True,
                'query':{
                    'recentAnalyses.KeywordCombinationAll /300 v1':{
                        '$exists': True
                     }
                 }
             }}
          ]
      }
}}

使用 POSTMAN 和 GET 请求对xxx.xxx.xxx.xxx:5000/geoAggr?aggregate={"$lon":"10.5","$lat":"10.5","$maxDistance":100000} 进行查询。

如果$lon 和$lat 被硬编码值替换,那么聚合器就可以完美运行,所以这似乎与变量放入聚合器的方式有关。我的第一个想法是这些数字可能会被解释为字符串,但 $maxDistance 参数可以按预期工作。

为什么它不能与变量一起使用?

【问题讨论】:

  • $ 在 MongoDB 中保留。能不能把变量名改一下试试?
  • 变量在到达 mongo 之前实际上已被 Eve 替换(我已经尝试过更改它们,这是基于 Eve 文档python-eve.org/features.html#mongodb-aggregation-framework 中的示例)。我现在已经找到了解决方案,不过感谢您的建议

标签: mongodb eve


【解决方案1】:

数组中的变量似乎没有被替换(我不确定这是否在所有情况下都是正确的,但在这种情况下是正确的),这是有道理的。

将'coordinates':['$lon', '$lat'] 替换为'coordinates':'$coords' 并使用xxx.xxx.xxx.xxx:5000/geoAggr?aggregate={"$coords":[10.5,10.5],"$maxDistance":100000} 进行访问。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 2018-08-25
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 2017-07-18
    • 1970-01-01
    • 2020-12-26
    相关资源
    最近更新 更多