mongodb - 使用 Spring Data 查找所有 mongo 查询

标签 mongodb spring-data

我正在使用 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/

相关文章:

php - MongoDB(或JSON)的Word Cities/Towns/Countries数据库?

MongoDB:如何获取子文档字段值的不同列表?

java - 如何部分使用 Hazelcast 和 Spring Data?

spring - ElementCollection 和 "failed to lazily initialize a collection of role"异常

java - Spring 启动 + 甲骨文 : Hibernate dialect is 9i when in properties it is set to 10g

mongodb - 实时数据馈送 + NodeJS 和 MongoDB

PHP MongoDB $exist 不工作

mongodb - Docker Compose MongoDB docker-entrypoint-initdb.d 不起作用

java - application.properties 中 spring.jpa.hibernate.ddl-auto=update 的问题

java - Solr数据配置中如何从多表中获取数据