How would you go about searching for an element inside an array stored in a json
column?
If I have a JSON document like this, stored in a JSON column named blob
:
{"name": "Wolf",
"ids": [185603363281305602,185603363289694211]}
what I'd like to be able to do is something like:
SELECT * from "mytable" WHERE 185603363289694211 = ANY("blob"->'ids');
and get all matching rows out. But this doesn't work because "blob"->'ids'
returns JSON values, not Postgres arrays.
I'd also like to build an index on the individual IDs, if that's possible.
This one of the nice things about MongoDB: you can just match elements within an array without doing anything special.