How can I sort by a nested JSONB attribute in sequelize
up vote
0
down vote
favorite
I want to sort a sequelize query by a nested attribute of a JSONB object.
I have a model like that:
sequelize.define('product', {
available: { type: Sequelize.BOOLEAN },
price: { type: Sequelize.JSONB },
...
}
where price is kind of this:
price = {
EUR: 1.2,
CHF: 1.3,
}
I want to sort a findAll query by price.EUR. I hoped that this works:
product.findAll({ where: { available: true }, order: [['price.EUR', 'ASC']] }
But I get an unsorted array.
The underlying DB is postgres.
A solution is to sort the result programmatically in javascript (array._prototype.sort()). But I wonder if it is possible with a sequelize query.
javascript postgresql sequelize.js
add a comment |
up vote
0
down vote
favorite
I want to sort a sequelize query by a nested attribute of a JSONB object.
I have a model like that:
sequelize.define('product', {
available: { type: Sequelize.BOOLEAN },
price: { type: Sequelize.JSONB },
...
}
where price is kind of this:
price = {
EUR: 1.2,
CHF: 1.3,
}
I want to sort a findAll query by price.EUR. I hoped that this works:
product.findAll({ where: { available: true }, order: [['price.EUR', 'ASC']] }
But I get an unsorted array.
The underlying DB is postgres.
A solution is to sort the result programmatically in javascript (array._prototype.sort()). But I wonder if it is possible with a sequelize query.
javascript postgresql sequelize.js
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to sort a sequelize query by a nested attribute of a JSONB object.
I have a model like that:
sequelize.define('product', {
available: { type: Sequelize.BOOLEAN },
price: { type: Sequelize.JSONB },
...
}
where price is kind of this:
price = {
EUR: 1.2,
CHF: 1.3,
}
I want to sort a findAll query by price.EUR. I hoped that this works:
product.findAll({ where: { available: true }, order: [['price.EUR', 'ASC']] }
But I get an unsorted array.
The underlying DB is postgres.
A solution is to sort the result programmatically in javascript (array._prototype.sort()). But I wonder if it is possible with a sequelize query.
javascript postgresql sequelize.js
I want to sort a sequelize query by a nested attribute of a JSONB object.
I have a model like that:
sequelize.define('product', {
available: { type: Sequelize.BOOLEAN },
price: { type: Sequelize.JSONB },
...
}
where price is kind of this:
price = {
EUR: 1.2,
CHF: 1.3,
}
I want to sort a findAll query by price.EUR. I hoped that this works:
product.findAll({ where: { available: true }, order: [['price.EUR', 'ASC']] }
But I get an unsorted array.
The underlying DB is postgres.
A solution is to sort the result programmatically in javascript (array._prototype.sort()). But I wonder if it is possible with a sequelize query.
javascript postgresql sequelize.js
javascript postgresql sequelize.js
asked Nov 13 at 16:51
Jochen Kunze
314
314
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
try this:
roduct.findAll({ where: { available: true }, order: [[sequelize.literal('price->>'EUR''), 'ASC']] }
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
try this:
roduct.findAll({ where: { available: true }, order: [[sequelize.literal('price->>'EUR''), 'ASC']] }
add a comment |
up vote
0
down vote
try this:
roduct.findAll({ where: { available: true }, order: [[sequelize.literal('price->>'EUR''), 'ASC']] }
add a comment |
up vote
0
down vote
up vote
0
down vote
try this:
roduct.findAll({ where: { available: true }, order: [[sequelize.literal('price->>'EUR''), 'ASC']] }
try this:
roduct.findAll({ where: { available: true }, order: [[sequelize.literal('price->>'EUR''), 'ASC']] }
answered Nov 16 at 5:56
Dmytro Mysak
300213
300213
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285885%2fhow-can-i-sort-by-a-nested-jsonb-attribute-in-sequelize%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown