【问题标题】:Find all mongo query with Spring Data使用 Spring Data 查找所有 mongo 查询
【发布时间】:2012-05-30 02:51:37
【问题描述】:

我正在开发一个带有 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 不是问题。

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

【问题讨论】:

  • 您知道吗?您可以添加自己的答案并接受它(并且可能会获得赞成票),而不是用答案编辑问题。在当前状态下,这个问题出现在 unanswered 部分,这是不正确的,因为有解决方案。谢谢:-)

标签: mongodb spring-data


【解决方案1】:

我发现问题而不是通过

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

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

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

而且效果很好:)

【讨论】:

  • 但是有什么区别呢? AFAIK,在后台,Java 无论如何都会将 var-args 转换为数组......
猜你喜欢
  • 1970-01-01
  • 2022-01-17
  • 2017-05-02
  • 2012-07-21
  • 2013-05-20
  • 1970-01-01
  • 1970-01-01
  • 2019-10-14
  • 2019-12-01
相关资源
最近更新 更多