【问题标题】:Spring-MongoDB geonear not working with extra fieldsSpring-MongoDB geonear 不使用额外的字段
【发布时间】:2013-12-03 02:52:26
【问题描述】:

我在 MongoDB 中有一些地理空间数据,我想查找靠近用户位置并符合某些条件的地点列表。

这是我的文档结构:

{ "_id" : { "$oid" : "528af043d1a2760efcd2fd2f" }, "city" : "Miami", "name" : "Some Place", "tickets" : 61, "zipcode" : "33142", "type" : "amusement park", "state" : "FL", "location" : [ -80.24, 25.8092 ]}
{ "_id" : { "$oid" : "528af043d1a2760efcd2fd30" }, "city" : "Miami", "name" : "Other place", "tickets" : 15, "zipcode" : "33150", "type" : "theatre", "state" : "FL", "location" : [ -80.2094, 25.8587 ]}

现在我正在尝试通过纬度、经度来查找有门票并且是某种类型的地方。当我只使用带坐标的 NearQuery(不添加查询)时,我得到了结果,但是当我添加查询对象时,我得到的是空列表。

这是我的代码:

public GeoResults<Place> find(double lat, double lon, int tickets, String type) {
    Point point = new Point(lon, lat);

    Query query = new Query(Criteria.where("tickets").gt(tickets).
            and("type").is(type));
    NearQuery nearQuery = NearQuery.near(point).maxDistance(new Distance(10, Metrics.KILOMETERS));
    nearQuery.query(query);
    GeoResults<Place> repoData = repo.findByLocationNear(point, new Distance(10, Metrics.KILOMETERS));

    GeoResults<Place> data = mongoTemplate.geoNear(nearQuery, Place.class);         
    List<Place> testData = mongoTemplate.find(query, Place.class);

    return data;
}

从上面的代码,GeoResults 数据没有内容,而 List testData 返回正确的结果(但不使用空间信息)。如果我使用存储库,我可以获得地点列表,但这不考虑其他参数。

提前致谢

【问题讨论】:

    标签: spring mongodb geospatial spring-data spring-data-mongodb


    【解决方案1】:

    我自己找到了一种方法。我将代码一直调试到 Spring-mongodb 库中,发现“num”设置为 0,“fields”设置为 null。因此,在调试过程中,我手动添加了字段,并为它应该检索的文档数量提供了一个值。我的假设是它应该返回符合条件的所有字段和任意数量的文档。

    这是创建查询的更新代码:

    Query query = new Query(Criteria.where("tickets").gt(tickets).
                and("type").is(type));
    query.fields().include("city").include("name").include("tickets").
    include("type").include("state").include("address");
    
    NearQuery nearQuery = NearQuery.near(point).maxDistance(new Distance(radius, Metrics.KILOMETERS));
    nearQuery.query(query);
    nearQuery.num(100);
    
    GeoResults<Place> data = mongoTemplate.geoNear(nearQuery, Place.class); 
    

    【讨论】:

      【解决方案2】:

      我也遇到了同样的问题,经过长时间的挣扎,我终于找到了问题的根本原因。

      1. NearQuery.near(point).maxDistance(distance); //returns all possible results
      2. NearQuery.near(point).maxDistance(distance).num(20); //returns 20 nearest results   
      3. NearQuery.near(point).maxDistance(distance).num(20).query(new Query()); //returns 0!! results
      4. NearQuery.near(point).maxDistance(distance).query(new Query()).num(20); //returns 20 nearest results
      5. NearQuery.near(point).maxDistance(distance).query(new Query().limit(20)); //returns 20 nearest results
      6. NearQuery.near(point).maxDistance(distance).query(new Query().limit(20)).num(5); //returns 5!! nearest results
      

      第三个NearQuery有0个结果的原因是因为NearQuery类的.query(...)部分是这样做的:

      public NearQuery query(Query query) {
          this.query = query;
          this.skip = query.getSkip();
          this.num = query.getLimit();
          return this;
      }
      

      NearQuery 的参数numQuerylimit 覆盖。 .num(...).query(...) 的顺序非常重要。 您必须添加.num(20) 之后 .query(...) like (4) 或使用 query.limit(20) like (5)。 如果将两者结合起来,则最后一个值获胜 (6)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-17
        • 2019-05-15
        • 2023-03-18
        • 2015-11-18
        • 2021-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多