简体   繁体   English

Spring-MongoDB geonear不使用额外的字段

[英]Spring-MongoDB geonear not working with extra fields

I have some geospatial data in MongoDB and I want to find list of places which are near User's location and match certain criteria. 我在MongoDB中有一些地理空间数据,我想查找靠近用户位置并符合特定条件的地点列表。

Here is my document structure: 这是我的文档结构:

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

Now I am trying to find places through latitude, longitude, which have tickets available and are of certain kind. 现在我正在尝试通过纬度,经度找到可以获得门票且具有特定种类的地方。 When I just use NearQuery with coordinates (without adding query), I am getting results, but when I add query object I am getting empty list. 当我只使用带有坐标的NearQuery(不添加查询)时,我得到了结果,但是当我添加查询对象时,我得到了空列表。

Here is my code: 这是我的代码:

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

From above code, GeoResults data has no contents while List testData returns proper results(but does not uses spatial information). 从上面的代码中,GeoResults数据没有内容,而List testData返回正确的结果(但不使用空间信息)。 If I use repository, I can get list of places but that does not takes additional parameters into consideration. 如果我使用存储库,我可以获取地点列表,但不考虑其他参数。

Thanks in Advance 提前致谢

I found one way to do it myself. 我找到了一种自己做的方法。 I debugged code all the way into Spring-mongodb library and saw that 'num' was set to 0 and 'fields' was set to null. 我一直调试代码到Spring-mongodb库,看到'num'设置为0,'fields'设置为null。 So during debugging I added fields manually and provided a value to number of documents it should retrieve. 因此,在调试过程中,我手动添加了字段,并为应检索的文档数量提供了值。 My assumption was that it should return all fields and any number of documents matching the criteria. 我的假设是它应该返回所有字段和符合条件的任何数量的文档。

Here is the updated code to create a query: 以下是创建查询的更新代码:

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

I had the same problem and after a long struggle I finally found the root cause of my problem. 我遇到了同样的问题,经过长时间的斗争,我终于找到了问题的根本原因。

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

The reason why the third NearQuery has 0 results is due to the fact that the .query(...) part of the NearQuery class does this: 第三个NearQuery有0个结果的原因是由于NearQuery类的.query(...)部分这样做:

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

The parameter num of NearQuery is overridden by the limit of the Query . NearQuery的参数numQuerylimit覆盖。 The order of .num(...) and .query(...) is really important. .num(...).query(...)的顺序非常重要。 You have to add .num(20) after .query(...) like (4) or use query.limit(20) like (5). 您必须 .query(...) .num(20) 之后添加.num(20) 如(4)或使用query.limit(20)如(5)。 If you combine both, the last value wins (6). 如果两者结合,则最后一个值获胜(6)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM