如何使用typeorm进行排序?
How to sort with typeorm?
数据库包含以下数据:
[
{id: 1, username: "test", amount: 2},
{id: 2, username: "example", amount: 0},
{id: 3, username: "hello", amount: 1},
{id: 4, username: "fsa", amount: 0},
{id: 5, username: "aas", amount: 10},
]
现在我需要使用 typeorm 查询来检索数据,但我需要根据数量字段对其进行排序并且限制只有 3 个用户,所以我应该得到这个:
[
{id: 5, username: "aas", amount: 10},
{id: 1, username: "test", amount: 2},
{id: 3, username: "hello", amount: 1},
]
如何查询?
使用 find
方法解决以下问题。查看 find options 的 TypeORM 文档以获取更多信息和其他选项。
userRepository.find({
take: 3,
order: {
amount: "ASC" // "DESC"
}
}
数据库包含以下数据:
[
{id: 1, username: "test", amount: 2},
{id: 2, username: "example", amount: 0},
{id: 3, username: "hello", amount: 1},
{id: 4, username: "fsa", amount: 0},
{id: 5, username: "aas", amount: 10},
]
现在我需要使用 typeorm 查询来检索数据,但我需要根据数量字段对其进行排序并且限制只有 3 个用户,所以我应该得到这个:
[
{id: 5, username: "aas", amount: 10},
{id: 1, username: "test", amount: 2},
{id: 3, username: "hello", amount: 1},
]
如何查询?
使用 find
方法解决以下问题。查看 find options 的 TypeORM 文档以获取更多信息和其他选项。
userRepository.find({
take: 3,
order: {
amount: "ASC" // "DESC"
}
}