2017-09-11 101 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'}}]} 
          }, 

        } 
相關問題