首页 > 解决方案 > 在 Kotlin 中为 findAllOrderByFieldDesc 生成 mongodb 查询

问题描述

我正在尝试为我的实体构建简单的自动生成的查询,以按降序对某个数字字段进行排序,如下所示:

@Document(collection = "my_coll")
class MyEntity (

    @Id
    val id: ObjectId = ObjectId.get(),

    val someField: Number

)

...

interface MyEntityRepository : MongoRepository<MyEntity, String> {
//this is failing query I try to build
    fun findAllOrderbySomeFieldDesc(): List<MyEntity>
}

不幸的是我得到:

org.springframework.data.mapping.PropertyReferenceException: No property findAllOrderbySomeFieldDesc found for type MyEntity

我怎样才能做到这一点?

更新:

根据以下功能应该可以工作

fun findByOrderBySomeFieldDesc(): List<MyEntity>
//unfortunately it fails again for me with
//No property findAllOrderbySomeFieldDesc found for type MyEntity

我目前的工作解决方案是使用带有 Sort 参数的排序

myEntityRepository.findAll(Sort.by(Sort.Direction.DESC, "someField"))

问题仍然是如何使用 spring 自动生成方法实现相同的功能?

标签: mongodbkotlinspring-data

解决方案


根据Officials Docs查询方法主题关键字在您的情况下应以“By”结尾,因此方法名称应为findAllByOrderBySomeFieldDesc()


推荐阅读