0

I have looked extensively for an answer but could not find a simple solution.

I have a table that contains a column subscriptionHistory

The data can look like so:

[
  {
    "fromDate": "2023-01-24T10:11:57.150Z",
    "userSubscribedToo": "EuuQ13"
  },
  {
    "fromDate": "2022-01-24T10:11:57.150Z",
    "tillDate": "2022-02-24T22:59:59.999Z",
    "userSubscribedToo": "a4ufoAB"
  }
]

I'm trying to find the records of the subscriptions.

In Mongo we do

'subscriptionHistory.$.userSubscribedToo' = 'a4ufoAB'

Nice and easy.

I'm using PostgreSQL and Sequelize,

The following doesn't work.

const totalEarnings = await SubscriptionToken.count({
  where: {
    'subscriptionHistory.$.userSubscribedToo': user.id,
  },
});

Neither do any direct queries

SELECT *
FROM vegiano_dev."subscription-tokens"
WHERE "subscriptionHistory"->>'userSubscribedToo' = 'a4ufoAB'
--WHERE "subscriptionHistory" @> '{"userSubscribedToo": "a4ufoAB"}'

Not sure where to go now :-/

2
  • What is the column type of subscriptionHistory? Commented Jan 25, 2023 at 10:34
  • @vicki JSONB type Commented Jan 25, 2023 at 10:40

1 Answer 1

1

You can use a JSON path condition with the @@ (exists) operator:

select *
from vegiano_dev."subscription-tokens"
where "subscriptionHistory" @@ '$[*].userSubscribedToo == "a4ufoAB"'

The @> will work as welll, but because subscriptionHistory is an array, you need to use an array with that operator:

where "subscriptionHistory" @> '[{"userSubscribedToo": "a4ufoAB"}]'

This assumes that subscriptionHistory is defined as jsonb which it should be. If it's not, you need to cast it: "subscriptionHistory"::jsonb

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.