java - spring data mongodb 地理空间查询

标签 java mongodb spring-boot spring-data

我一直在尝试使用地理空间查询将数据提取到 pojo 中,但没有成功。

这是我的 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
        ]
    }
}

下面的 mongo 查询在 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);
}

和 Controller

@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 来获取这些数据。我感觉问题出在我在实体中注释位置字段的方式。我有什么遗漏的吗?任何帮助表示赞赏。

最佳答案

Spring MongoDb 地理空间查询:已经测试良好。

   public List<MachineDetails> searchbylocation(String lat, String longt, String maxDistance) {
            BasicQuery query1 = new BasicQuery("{geoLocation:{ $near: { $geometry: { type: 'Point',coordinates: ["+ lat+","+ longt+" ] }, $minDistance: 10, $maxDistance: "+maxDistance+"}}}");

            List<MachineDetails> venues1 = mtemplate.find(query1, MachineDetails.class);

return venues1;
}

关于java - spring data mongodb 地理空间查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45529257/

相关文章:

java - 将手机的 Activity 转换为平板电脑的 Fragment

java - 包 org.springframework.boot 不存在

java - Hibernate多对多: Criteria for looking up all class A which DO NOT contain class B

json - 如何使用req.body将JSON数组数据保存到mongoose?

mongodb - 蒙戈克司机 : how to query based on ISOdate?

java - Spring 启动 : Autowired fields are none

spring - 如何使用Spring boot向Prometheus中的目标添加标签?

java - 按下按钮的actionPerformed()

java - 用于枚举字段的 mongoDb 和 spring 数据自定义转换器

java - Spring Boot 忽略 Rest Controller 中的 Jackson 注释