1

I am having some issues retrieving the correct date within a particular radius from MongoDB. I have a json example shown at the bottom. My goal is to search for all items close to a defined geolocation as well as filtering on a starttime. This also includes creating the correct index. There is currently not a lot guidance out and about. Thanks in advance for any help.

In summary, what I'm trying to do is:

  • retrieve all items from MongoDB that are within 3 miles of a provided geolocation
  • filter results for items with a starttime after x but before y.
  • Create an index that works for my query.

Regards, Lance

If I just wanted to query on dates I could do the following:

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();
}

If I just wanted to query on GeoLocation I could do the following:

//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
               }  )

I attempted this with Jongo as well but without any success:

   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);

My sample 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"
    }
  ]
}
LanceP
  • 974
  • 1
  • 9
  • 15
  • Solved. I simply needed to use the correct maxDistance; Double ONE_DEGREE_IN_MILES = 69.11; //1° of latitude = about 69.11 miles – LanceP Mar 12 '13 at 08:45

1 Answers1

3

Solved. To get Jongo to work I simply needed to use the correct maxDistance. Instead of EarthRadiusInMILES, I should have defined this; Double ONE_DEGREE_IN_MILES = 69.11;

Note: 1° of latitude = about 69.11 miles

LanceP
  • 974
  • 1
  • 9
  • 15