2017-09-11 37 views
1

我正在开发一个非常轻量级的API,使用python前夕访问一个mongodb数据库。数据库中的每个文档都有一个geom字段,并且该字段上有一个2d球形索引。postman(和python前夕)在mongodb中提供不同的结果给相同的查询

当我运行此查询在蒙戈它完美的作品,并很快

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"}} 

我有一个基本的在夏娃建立的模式,部分工作。它仅返回_id,而不是作为查询的一部分创建的距离字段。虽然我运行的假设,这将工作一旦我有postman正确的语法。

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'}}]} 
          }, 

        } 
+2

不要使用它自己,但可以肯定从[文档的快速细读(HTTP:/ /python-eve.org/features.html#mongodb-aggregation-framework)你实际上是想在配置中指定“pipleline”,而不是URL的一部分。 URL参数似乎用于“变量替换”中。因此,在端点上设置管道似乎是合乎逻辑的,而不是将整个管道作为参数传递。 –

+0

谢谢尼尔,但仍然得到同样的问题,被忽略的查询和所有文档被返回 – SAB

回答

0

排序工作,但忽略了架构...但我猜这一个不同的问题。

移动聚合管道进入该项目,并删除方括号“$ 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'}}]} 
          }, 

        } 
相关问题