【问题标题】:Annotation argument error while using $slice in Spring MongoDB Repository with @Query in Kotlin在 Spring MongoDB Repository 中使用 $slice 和 Kotlin 中的 @Query 时出现注释参数错误
【发布时间】:2020-05-14 03:58:06
【问题描述】:

我正在将 Spring Boot Kotlin 与 MongoDB 一起使用。在这个项目中,我有一个类,它有一个名为 pointsEntry 的数组属性。在我自己的 findAll 方法中检索它们时,我一直在尝试使用运算符 $slice 仅获取所有列表的第一个 pointsEntry。在我的 Mongo 客户端中尝试此查询时有效:

db.getCollection('collectionName').find( {}, { "pointsEntry" : { $slice: 1 } } )

但是,我在我的 Spring Boot 项目中使用 @Query 注释尝试了类似的方法,但它总是报错

注解参数必须是编译时常量

@Query(value = "{}",fields = "{pointsEntry: {'$slice' : ?0}}")
fun findAll() : List<DocumentName>

【问题讨论】:

    标签: spring mongodb spring-boot kotlin


    【解决方案1】:

    pointsEntry 字段中缺少 ' 单引号

    @Query(value = "{}", fields = "{'pointsEntry': {'$slice' : ?0}}")
    

    【讨论】:

    • 我已经这样做了,但没有工作。但是,如果我在 java 类中使用相同的查询,那么它工作正常。问题仅与 kotlin 类有关。
    猜你喜欢
    • 2018-08-25
    • 2014-05-21
    • 2019-03-20
    • 2017-11-20
    • 2022-06-30
    • 2019-05-12
    • 2020-11-28
    • 1970-01-01
    • 2018-05-11
    相关资源
    最近更新 更多