2017-08-06 25 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数据无法确定要查询哪个集合。希望这可以帮助某人,因为我刚刚浪费了几天的时间。