'Querying against nested JSONB array in hash

I have a table with the following structure -

Column Name | Data Type
--------------------------
user_id     | uuid

profile     | jsonb

An example profile field would be something like -

{ "data": { "emails": [ "[email protected]", 
                        "[email protected]" ] } }

I want to query this table for users who have emails that exceed a certain string limit. I think to do this I can compare the nested array to a regex match. But I'm having trouble.

jsonb_path_query_array(User.where("profile -> 'emails', '$[*] ? (@ like_regex "^.{321,}$")'")

I've tried this, but it is not working.

Thank you!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source