在Spring MongoDB存储库中使用$slice和@Query

在Spring MongoDB存储库中使用$slice和@Query,spring,mongodb,spring-boot,jhipster,Spring,Mongodb,Spring Boot,Jhipster,我在MongoDB中使用Spring Boot。在这个项目中,我有一个域类ControlVariable,它有一个名为ControlVarEntries的数组属性。在我自己的findAll方法中检索ControlVar时,我一直试图使用操作符$slice只获取所有ControlVar的最后五个controlVarEntries。在我的Mongo客户端中尝试时,此查询有效: db.getCollection('controlVariable').find( {}, { controlVarEntr

我在MongoDB中使用Spring Boot。在这个项目中,我有一个域类ControlVariable,它有一个名为ControlVarEntries的数组属性。在我自己的findAll方法中检索ControlVar时,我一直试图使用操作符$slice只获取所有ControlVar的最后五个controlVarEntries。在我的Mongo客户端中尝试时,此查询有效:

db.getCollection('controlVariable').find( {}, { controlVarEntries: { $slice: -5 } } )
然而,我在我的Spring Boot项目中使用@Query注释尝试了类似的方法,但无法检索到相同的结果。例如,这将返回所有条目:

@Repository
public interface ControlVariableRepository extends MongoRepository<ControlVariable, String> {

    @Query("{ controlVarEntries: { $slice: ?0 } }")
    Page<ControlVariable> findAllLimitedNumberOfEntriesQuery(Pageable pageable, Integer numberOfEntries);

}
@存储库
公共接口ControlVariableRepository扩展了MongoRepository{
@查询({controlVarEntries:{$slice:?0}}”)
Page FindAllimitedNumber of Entries查询(可分页、可分页、整数个条目);
}
我想知道我的查询是否错误,或者我是否使用了正确的语法


谢谢

您必须在@Query注释中标识投影文档

试一试

@Query(value=“{}”,fields=“{'controlVarEntries':{'$slice':?0}”)
Page FindAllimitedNumber of Entries查询(可分页、可分页、整数个条目);

您应该使用
$slice:?1
不幸的是,我相信它也不起作用。我还应该说,我没有使用任何参数:@Query({},{controlVarEntries:{$slice:5}}),但它也错了。
@Query(value="{}", fields = "{'controlVarEntries': { '$slice': ?0 } }")
Page<ControlVariable> findAllLimitedNumberOfEntriesQuery(Pageable pageable, Integer numberOfEntries);