2013-03-11 5 views
1

MongoDB에서 특정 반경 내의 올바른 날짜를 검색하는 데 문제가 있습니다. 아래쪽에 json 예제가 있습니다. 내 목표는 시작 시간에 필터링뿐만 아니라 정의 된 Geolocation에 가까운 모든 항목을 검색하는 것입니다. 여기에는 정확한 색인 작성도 포함됩니다. 현재 많은 안내가 나오지 않습니다. 모든 도움을 미리 감사드립니다. 요약Java/Mongo geoLocation과 지정된 시간 내에 쿼리

은, 난 할 노력하고있어입니다 :

  • 는 X 후하지만 전에 STARTTIME있는 항목에 대해 제공되는 위치 정보의 3마일
  • 필터 결과 내에있는 MongoDB의 모든 항목을 검색 와이.
  • 내 쿼리에 적합한 인덱스를 만듭니다.

감사합니다, 랜스 난 그냥 다음 작업을 수행 할 수있는 날짜를 조회하고 싶었다면

: 난 그냥 다음을 수행 할 수있는 위치 서비스 조회하고 싶었다면

DateTime maxdateTime = new DateTime(); //Joda Time 
//Adjust my datetime, simply add 2 hours to the time 
DateTime mindateTime = new DateTime(); Joda Time 
//Adjust my datetime, subtract 1 day 
Date maxDate = new Date(maxdateTime.getMillis()); 
Date minDate = new Date(mindateTime.getMillis()); 

DBCollection coll = db.getCollection("BikeRides");  
coll.ensureIndex({ startTime:1, }) 
BasicDBObject query = new BasicDBObject(); 
query.put("startTime", BasicDBObjectBuilder.start("$gte", minDate).add("$lte", maxDate).get()); 

DBCursor cursor = coll.find(query); 

try { 
    while(cursor.hasNext()) { 
     System.out.println(cursor.next()); 
    } 
} finally { 
    cursor.close(); 
} 

을 :

//A GeoLoc is passed into this method 
int distance = 3; 
int earthRadius = 3963.192; 
DBCollection coll = db.getCollection("BikeRides");  
coll.ensureIndex({ Location.GeoLoc : "2d" }) 
db.runCommand({ geoNear: "BikeRides", 
       nearSphere: [ "Location.GeoLoc.latitude": geoLoc.longitude, "Location.GeoLoc.longitude": geoLoc.latitude ] 
       maxDistance: distance/earthRadius 
       } ) 

나는 이것을 시도했지만 아무런 성공도하지 못했다 :

,
DateTime nowDateTime = new DateTime(DateTimeZone.UTC); // Joda time 
    DateTime maxStartTime = nowDateTime.plusMinutes(TIME_IN_MINUTES); 
    DateTime minStartTime = nowDateTime.minusDays(1); //This will cut out most old bike rides 
    Long now = nowDateTime.toInstant().getMillis(); 
    Long max = maxStartTime.toInstant().getMillis(); 
    Long min = minStartTime.toInstant().getMillis(); 
    //Get the objects using Jongo 
    MongoCollection bikeRidesCollection =  MongoDatabase.Get_DB_Collection(MONGO_COLLECTIONS.BIKERIDES, "Location.GeoLoc"); 
    //Currently not placing a limit on this result. If we find this is an issue we can add later. 

    bikeRidesCollection.ensureIndex("{Location.GeoLoc: '2d', RideStartTime: 1}"); 
    Iterable<BikeRide> all = bikeRidesCollection 
      .find("{Location.GeoLoc: {$near: [#, #], $maxDistance: #}, RideStartTime: {$lte: #, $gte: #}}", //, $maxDistance: # 
       location.getGeoLoc().longitude, 
       location.getGeoLoc().latitude, 
       RADIUS_IN_MILES/EarthRadiusInMILES, 
       max , 
       min) 
      .as(BikeRide.class); 
    List<BikeRide> closeBikeRides = Lists.newArrayList(all); 

내 샘플 JSON :

{ 
    "BikeRides": [ 
    { 
     "StartTime": "2013-03-08T00:01:00.000 UTC", 
     "bikeRideName": "Strawberry Ride", 
     "id": "513afc2d0364b81b8abfa86e", 
     "location": { 
     "city": "Portland", 
      "geoLoc": { 
      "longitude": "-122.71446990966797", 
      "latitude": "45.49216842651367" 
     }, 
     "state": "OR", 
     "streetAddress": "4214 SE 36th" 
     }, 
     "startTime": "2013-03-07T16:01:00-08:00" 
    } 
    ] 
} 
+0

해결되었습니다. 나는 단지 정확한 maxDistance를 사용할 필요가 있었다. 두 번 ONE_DEGREE_IN_MILES = 69.11; // 위도 1 ° = 약 69.11 마일 – LanceP

답변

3

를 해결. Jongo가 작동하도록하려면 올바른 maxDistance를 사용하기 만하면됩니다. EarthRadiusInMILES 대신에 이것을 정의해야합니다. 두 번 ONE_DEGREE_IN_MILES = 69.11;

참고 : 위도 1 ° = 약 69.11 마일