gpt4 book ai didi

javascript - 如何在 sequelize 中按嵌套的 JSONB 属性排序

转载 作者:行者123 更新时间:2023-11-29 12:07:42 25 4
gpt4 key购买 nike

我想通过 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com