2014-07-02 70 views
0

我正在使用spring-data-mongodb。spring-data-mongodb中geoNar中的distanceField

  1. 我可以使用聚合激發一個geoNear查詢。
  2. 我可以在geoNear查詢中指定distanceField,就像我們在mongodb中做的那樣。

下面是示例MongoDB的查詢: -

([ 
{ 
    $geoNear: { 
     near: [ 
      28.45, 
      77.34 
     ], 
     distanceField: "distance", 
     maxDistance: 20, 
     num: 5, 
     query: { 
      $or: [ 
       { 
        cuisines: { 
         '$in': [ 
          'Mughlai', 
          'NorthIndian' 
         ] 
        } 
       } 
      ] 
     } 
    } 
}, 
{ 
$unwind: '$ProducerTimeSlots' 
} 
]) 

回答

0

您需要延長匯聚操作能力,GeoNearOperations類沒有distanceField變量。

public class GeoNearOperationExt implements AggregationOperation { 

private NearQuery nearQuery; 

private String distanceField = "distance"; 

public GeoNearOperationExt(NearQuery nearQuery) { 
    this.nearQuery = nearQuery; 
} 

/** 
* Default is distance 
* @return 
*/ 
public String getDistanceField() { 
    return distanceField; 
} 

/** 
* Set distanceField value Default is distance 
* @param distanceField 
*/ 
public void setDistanceField(String distanceField) { 
    this.distanceField = distanceField; 
} 



@Override 
public DBObject toDBObject(AggregationOperationContext context) { 
    DBObject dbObject = context.getMappedObject(nearQuery.toDBObject()); 
    dbObject.put("distanceField", distanceField); 
    return new BasicDBObject("$geoNear", dbObject); 
} 
} 

要在聚合中使用它,只需將其包含在聚集操作數組中,即將參數傳遞給聚合函數。