简体   繁体   English

春季数据mongo地理空间查询

[英]spring data mongo geospatial query

I have been trying to use a geospatial query to fetch data into a pojo with no success. 我一直在尝试使用地理空间查询将数据获取到pojo中,但没有成功。

Here is an example data in my monogdb collection 这是我的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
        ]
    }
}

The below mongo query works perfectly fine in the mongoshell 下面的mongo查询在mongoshell中可以很好地工作

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

  } 
  )
  .pretty();

Here is the pojo that I have been trying to fetch the data into 这是我一直试图将数据获取到的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
   }

Here is the repository I have been using 这是我一直在使用的存储库

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

and the 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;
    }
}

Every time I try it I get no results. 每次尝试都没有结果。 I am sure that I have data in the given point and nearby locations and I was even able to use the mongoshell to fetch those. 我确信我在给定点和附近位置有数据,甚至可以使用mongoshell来获取这些数据。 I have feeling that the problem is with the way that I have annotated the location field in the entity. 我觉得问题出在我注释实体中位置字段的方式上。 Is there anything that I am missing? 有什么我想念的吗? Any help is appreiciated. 任何帮助表示赞赏。

Seems like there were more than one database in mongodb that had collections by the name 'trial'. 似乎mongodb中有多个数据库,这些数据库的名称为“ trial”。 I tried removing all those and now it is working fine. 我尝试删除所有这些,现在一切正常。 Spring data wasn't able to figure out which collection to be queried on as there were multiple collection with the same name in different databases. Spring数据无法确定要查询哪个集合,因为在不同的数据库中有多个具有相同名称的集合。 Hope this helps someone, cause I have just wasted a couple of days on this. 希望这对某人有帮助,因为我在此上浪费了几天。

Spring MongoDb Geo spatial query: It is tested well. 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM