作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想通过 JSONB 对象的嵌套属性对 sequelize 查询进行排序。
我有一个这样的模型:
sequelize.define('product', {
available: { type: Sequelize.BOOLEAN },
price: { type: Sequelize.JSONB },
...
}
价格是这样的:
price = {
EUR: 1.2,
CHF: 1.3,
}
我想按 price.EUR 对 findAll 查询进行排序。我希望这有效:
product.findAll({ where: { available: true }, order: [['price.EUR', 'ASC']] }
但是我得到了一个未排序的数组。
底层数据库是postgres。
解决方案是在 javascript (array._prototype.sort()) 中以编程方式对结果进行排序。但我想知道是否可以使用 sequelize 查询。
最佳答案
试试这个:
roduct.findAll({ where: { available: true }, order: [[sequelize.literal('price->>\'EUR\''), 'ASC']] }
关于javascript - 如何在 sequelize 中按嵌套的 JSONB 属性排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53285885/
我是一名优秀的程序员,十分优秀!