2017-08-06 60 views
0

我一直在嘗試使用地理空間查詢將數據提取到pojo中,但沒有成功。春季數據mongo地理空間查詢

這裏是我的monogdb收集的示例數據

{ 
    "_id" : ObjectId("597b8c9a21871eeabd5a1cf5"), 
    "amb_id" : 10, 
    "amb_number" : "KL25 8945", 
    "driver_name" : "Amal Shaji", 
    "driver_licence_id" : "12/4562/2017", 
    "ownership" : "Amrita Institute of Medical Science", 
    "ownership_address" : "Peeliyadu Road, Ponekkara, Edappally, Ernakulam", 
    "location" : { 
     "type" : "Point", 
     "coordinates" : [ 
      76.293485, 
      10.032871 
     ] 
    } 
} 

以下蒙戈查詢工作完全正常的mongoshell

db.trial.find( 
    { location : 
    { $near :{ 
    $geometry :{ 
     type : "Point" , 
     coordinates : [ 76.2 , 9.9 ] } , 
     $maxDistance : 20000   } 
     } 

    } 
) 
    .pretty(); 

這裏是我一直在試圖獲取數據的POJO到

@Document(collection = "trial") 
    public class Ambulance { 
     @Id 
     String id; 
     @Field("amb_id") 
     String ambulanceId; 
     @Field("amb_number") 
     String licensePlateNumber; 
     @Field("driver_name") 
     String driverName; 
     @Field("driver_licence_id") 
     String driverLicenseNumber; 
     @Field("ownership") 
     String ownerShip; 
     @Field("ownership_address") 
     String ownerShipAddress; 
     @GeoSpatialIndexed(name="Location") 
     Double[] location; 

     //setters and getters 
    } 

這是我一直在使用的存儲庫

@ComponentScan 
@Repository 
public interface AmbulanceRepo extends MongoRepository<Ambulance, String> { 
    GeoResults<Ambulance> findByLocationNear(Point p, Distance d); 
} 

和控制器

@RestController 
@CrossOrigin(origins = "http://localhost:4200") 
@RequestMapping("/") 
public class NearbyAmbulanceController { 

    private AmbulanceRepo ambulanceRepo; 

    @Autowired 
    public NearbyAmbulanceController(AmbulanceRepo repo){ 
     this.ambulanceRepo = repo; 
    } 

    @RequestMapping(value="/nearbyAmbulance",method = RequestMethod.POST) 
    @ResponseBody 
    public GeoResults<Ambulance> getAmbulanceDetails(
      @RequestBody LocationRequest locationRequest){ 
     System.out.println("lati "+locationRequest.getLatitude()+ " long "+locationRequest.getLongitude()+" d "+locationRequest.getDistance()); 
//  List<Ambulance> ambulanceList=this.ambulanceRepo.findByLocationNear(new Point(Double.valueOf(locationRequest.getLongitude()),Double.valueOf(locationRequest.getLatitude())),new Distance(locationRequest.getDistance(), Metrics.KILOMETERS)); 
     Point point = new Point(locationRequest.getLatitude(), locationRequest.getLongitude()); 
     Distance distance = new Distance(locationRequest.getDistance(), Metrics.KILOMETERS); 
     GeoResults<Ambulance> ambulanceList=this.ambulanceRepo.findByLocationNear(point,distance); 
     System.out.println(ambulanceList); 
     return ambulanceList; 
    } 
} 

每次我嘗試我沒有得到任何結果。我確信我在給定點和附近位置都有數據,我甚至可以使用mongoshell來獲取這些數據。我感覺問題在於我在實體中註釋了位置字段的方式。有什麼我失蹤?任何幫助appreiciated。

回答

0

似乎在mongodb中有多個數據庫,名稱爲'trial'。我試圖刪除所有這些,現在它工作正常。由於在不同的數據庫中有多個具有相同名稱的集合,因此Spring數據無法確定要查詢哪個集合。希望這可以幫助某人,因爲我剛剛浪費了幾天的時間。