2017-08-06 7 views
0

私は地理空間クエリを使用して、成功していないpojoにデータを取得しようとしています。ここでspring data mongo geospatial query

は、以下のmongo問合せはこちら

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

    } 
) 
    .pretty(); 

は、私がデータをフェッチしようとしているPOJOであるmongoshellに完全に正常に動作します私の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 
     ] 
    } 
} 

です〜

@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を使ってそれらを取得することさえできました。私は問題がエンティティの位置フィールドに注釈を付けた方法であると感じています。私が紛失しているものはありますか?どんな援助も感謝しています。

答えて

0

「trial」という名前のコレクションを持つmongodbに複数のデータベースがあるようです。私はそれらをすべて取り除こうとしましたが、今は正常に動作しています。 Springのデータは、異なるデータベースに同じ名前のコレクションが複数あるため、どのコレクションを照会するかを特定できませんでした。これが誰かを助けてくれることを願って、私はちょうどこれについて数日を無駄にしたのです。

関連する問題