mongodb - 在 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 自动生成方法实现相同的功能?
解决方案
根据Officials Docs查询方法主题关键字在您的情况下应以“By”结尾,因此方法名称应为findAllByOrderBySomeFieldDesc()
推荐阅读
- python - 找到单个值将落入哪个直方图箱的快速方法?
- javascript - 在 React 中选择图像并显示标题
- python - Dash 确保 url 参数和下拉选择之间的一致性
- json - 试图从带有液体的产品标签中删除一些字符以用于搜索引擎,但没有成功
- asp.net-core - 这是否有效:EF Core 3.1,多个 (0..1)-to-1 关系
- html - 浮动不断创建不需要的空间
- python - TypeError: 不支持的操作数类型 -: 'str' 和 'float'
- c++ - 在我的 C 代码中使用 scanf 进行命令行重定向
- ms-access - MS Access 表单中从 TextBox 到 SQL 的日期
- reactjs - 如何在安装时将用户名和密码添加到电子桌面应用程序?