4

我正在使用 spring 数据和 mongo db 数据库开发后端。

我得到了以下课程

@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 不是问题。

这是一个愚蠢的问题,但 Spring Data for Mongo 的文档和示例非常基础,任何帮助或一个好的教程或文档的链接都可能会有所帮助,谢谢!:)

4

1 回答 1

3

我发现了问题而不是通过

Query categoryQuery = new Query(new Criteria("category").all(category));

作为一个 int[] 类别,我传递了一个 idCategory,idCategory2

Query categoryQuery = new Query(new Criteria("category").all(idCategory,idCategory2));

而且效果很好:)

于 2012-06-10T03:03:08.727 回答