【问题标题】:Java/Mongo Query with geoLocation and within specified timeframe在指定时间范围内使用 geoLocation 进行 Java/Mongo 查询
【发布时间】:2013-03-11 22:23:08
【问题描述】:

我在从 MongoDB 检索特定半径内的正确日期时遇到了一些问题。我在底部显示了一个 json 示例。我的目标是搜索靠近定义地理位置的所有项目,并在开始时间进行过滤。这还包括创建正确的索引。目前没有太多的指导。提前感谢您的帮助。

总之,我想做的是:

  • 从 MongoDB 中检索位于所提供地理位置 3 英里范围内的所有项目
  • 筛选开始时间在 x 之后但在 y 之前的项目的结果。
  • 创建一个适用于我的查询的索引。

问候, 兰斯

如果我只想查询日期,我可以执行以下操作:

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

如果我只想查询 GeoLocation,我可以执行以下操作:

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

我也尝试过 Jongo,但没有成功:

   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"
    }
  ]
}

【问题讨论】:

  • 已解决。我只需要使用正确的 maxDistance;双 ONE_DEGREE_IN_MILES = 69.11; //1° 纬度 = 约 69.11 英里

标签: mongodb-query mongodb-java jongo


【解决方案1】:

解决了。为了让 Jongo 工作,我只需要使用正确的 maxDistance。我应该定义这个,而不是 EarthRadiusInMILES;双 ONE_DEGREE_IN_MILES = 69.11;

注意:纬度 1° = 约 69.11 英里

【讨论】:

    猜你喜欢
    • 2012-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-07
    相关资源
    最近更新 更多