Spring MongoDB geonear不使用额外字段
我在MongoDB中有一些地理空间数据,我想找到靠近用户位置并符合某些标准的位置列表 以下是我的文档结构:Spring MongoDB geonear不使用额外字段,spring,mongodb,geospatial,spring-data,spring-data-mongodb,Spring,Mongodb,Geospatial,Spring Data,Spring Data Mongodb,我在MongoDB中有一些地理空间数据,我想找到靠近用户位置并符合某些标准的位置列表 以下是我的文档结构: { "_id" : { "$oid" : "528af043d1a2760efcd2fd2f" }, "city" : "Miami", "name" : "Some Place", "tickets" : 61, "zipcode" : "33142", "type" : "amusement park", "state" : "FL", "location" : [ -80.24, 2
{ "_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;
}
public GeoResults find(双lat、双lon、int票据、字符串类型){
点=新点(lon、lat);
查询查询=新查询(条件.where(“票证”).gt(票证)。
以及(“类型”)。为(类型));
NearQuery NearQuery=NearQuery.near(point).maxDistance(新距离(10,Metrics.km));
nearQuery.query(查询);
GeoResults repoData=repo.findByLocationNear(点,新距离(10,Metrics.km));
GeoResults data=mongoTemplate.geoNear(nearQuery,Place.class);
List testData=mongoTemplate.find(查询,Place.class);
返回数据;
}
从上面的代码中,GeorgeSults数据没有内容,而List testData返回正确的结果(但不使用空间信息)。如果我使用存储库,我可以得到位置列表,但这并不考虑其他参数
提前谢谢我自己找到了一种方法。我一直在SpringMongoDB库中调试代码,发现'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);
Query Query=新查询(条件.where(“票证”).gt(票证)。
以及(“类型”)。为(类型));
query.fields().include(“城市”).include(“名称”).include(“票证”)。
包括(“类型”)。包括(“州”)。包括(“地址”);
NearQuery NearQuery=NearQuery.near(point).maxDistance(新距离(半径,度量单位.km));
nearQuery.query(查询);
nearQuery.num(100);
GeoResults data=mongoTemplate.geoNear(nearQuery,Place.class);
我也有同样的问题,经过长期的斗争,我终于找到了问题的根源
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
的参数num
被Query
的limit
覆盖。
.num(…)
和.query(…)
的顺序非常重要。
您必须在之后添加.num(20)
。
如果将两者合并,则最后一个值获胜(6)