【问题标题】:Postman (and python eve) providing different results to same query when done in mongodb在 mongodb 中完成时,邮递员(和 python eve)为相同的查询提供不同的结果
【发布时间】:2017-09-13 18:18:26
【问题描述】:

我正在使用 python eve 开发一个非常轻量级的 API,它可以访问一个 mongodb 数据库。数据库中的每个文档都有一个 geom 字段,并且该字段上有一个 2d 球体索引。

当我在 mongo 中运行此查询时,它可以完美且非常快速地运行

db.api.aggregate({"$geoNear": {"near": {"type": "Point", "coordinates": [-1.11, 51.69]}, "distanceField": "distance", "maxDistance": 100, "num": 2, "spherical": "true"}}).pretty()

但是当我在邮递员中运行它时,它只会返回所有内容并忽略查询

http://localhost:8090/data?aggregate={"$geoNear": {"near": {"type": "Point", "coordinates":  [-1.11, 51.69]}, "distanceField": "distance", "maxDistance": 100,"num": 2,"spherical": "true"}}

我在 Eve 中设置了一个基本架构,它部分有效。它只返回 _id 而不是作为查询的一部分创建的距离字段。虽然我假设一旦我的邮递员语法正确,这将起作用。

api_shema = {'_id': {'type': 'string'},
                      'distance': {'type': 'string'}
                      }

我也设置了这个项目

line_info_item = {'item_title': 'line_info',
                        'resource_methods': ['GET'],
                        'schema': api_shema,
                        'datasource': {
                            'source': 'api',
                            'filter': {'_type': 'line'}
                            }
                     }

最后添加了以下域

DOMAIN = {'line_info': line_info_item}

任何有关邮递员查询的帮助,或者如果您发现其余的任何错误,将不胜感激。

编辑:

我根据下面 Neil 的回答在端点上设置了管道,但它仍然忽略查询并返回所有内容。

DOMAIN = {'line_info': line_info_item,
            'aggregation': {
                'pipeline': [{
                    "$geoNear": {
                        "near": {
                            "type": "Point", 
                            "coordinates": ["$coords"]
                        },
                        "distanceField": "distance", 
                        "maxDistance": "$maxDist",
                        "num": 10,
                        "spherical": "true"
                    }
                }]
            }
        }

邮递员查询网址是

http://localhost:8090/data?aggregate={"$maxDist":500, "$coords":[-1.477307, 50.931700]}

编辑

有点工作,虽然忽略了架构......但猜那是一个不同的问题。

将聚合管道移动到项目中并删除“$coords”周围的方括号

river_relate_item = {'item_title': 'line_info_item',

                        'resource_methods': ['GET'],

                        'schema': api_shema,

                        'datasource': {
                            'source': 'api',
                            'filter': {'_type': 'line'},
                            'aggregation': {'pipeline': [{'$geoNear':{'near':{'type': 'point', 'coordinates': '$coords'},'distanceField': 'distance','maxDistance': '$maxDist','num': 1, 'spherical': 'true'}}]}
                            },

                    }

【问题讨论】:

  • 我自己不要使用它,但从quick perusal of the documentation 可以肯定你实际上是要在配置中指定“管道”,而不是作为 URL 的一部分。 URL 参数似乎用于“变量替换”。因此,在端点上设置管道,而不是将整个管道作为参数传递,这似乎是合乎逻辑的。
  • 感谢 Neil,但仍然遇到查询被忽略并返回所有文档的问题

标签: python mongodb postman eve


【解决方案1】:

有点工作,虽然忽略了架构......但猜那是一个不同的问题。

将聚合管道移动到项目中并删除“$coords”周围的方括号

river_relate_item = {'item_title': 'line_info_item',

                        'resource_methods': ['GET'],

                        'schema': api_shema,

                        'datasource': {
                            'source': 'api',
                            'filter': {'_type': 'line'},
                            'aggregation': {'pipeline': [{'$geoNear':{'near':{'type': 'point', 'coordinates': '$coords'},'distanceField': 'distance','maxDistance': '$maxDist','num': 1, 'spherical': 'true'}}]}
                            },

                    }

【讨论】:

    猜你喜欢
    • 2021-03-23
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多