我正在使用 spring 数据和 mongo db 数据库开发后端。
我得到了以下类(class)
@Document
public class Place
{
@Id
private String id;
@GeoSpatialIndexed
private Double[] location;
private int[] category;
//gets and sets
}
所以我想进行查询以获取具有所选类别的点附近的地点,所以我得到了这个:
public List<Place> getPlacesNear(Double[] location, int[] category){
NearQuery geoNear = NearQuery.near(location[0],location[1],Metrics.KILOMETERS).maxDistance(KM_DISTANCE);
Query categoryQuery = new Query(new Criteria("category").all(category));
geoNear.query(categoryQuery);
GeoResults<Place> geoNearResult = mongoTemplate.geoNear(geoNear, Place.class);
//return results
}
但这没有返回任何结果,我知道查询。
db.place.find( { category: { $all: [ categoryID, categoryID2 ] } } );
运行良好,geoNear 不是问题。
这是一个愚蠢的问题,但是 Mongo 的 Spring Data 的文档和示例非常基础,任何帮助或好的教程或文档的链接都可能有所帮助,谢谢! :)
最佳答案
我发现了问题而不是通过了
Query categoryQuery = new Query(new Criteria("category").all(category));
作为一个 int[] 类别,我传递了一个 idCategory,idCategory2
Query categoryQuery = new Query(new Criteria("category").all(idCategory,idCategory2));
效果很好:)
关于mongodb - 使用 Spring Data 查找所有 mongo 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10809409/