Postgres: Filter Using JSONB Objects
Introduction
The _contains, _contained_in, _has_key, _has_keys_any, _has_keys_all, _jsonb_path_exists and
_jsonb_path_match operators are used to filter based on JSONB columns.
For more details on JSONB operators and Postgres equivalents, refer to the API reference.
_contains
Fetch all authors living within a particular pincode (present in address JSONB column):
_contained_in
This example fetches all items where the "Tags" property contains the value "tag1" and is also contained in the list
["tag1", "tag2"].
_has_key
Fetch authors if the phone key is present in their JSONB address column:
_has_keys_any
Fetch users who have either "admin" or "editor" permissions:
_has_keys_all
Fetch user, where their id is "123", and check if the address has all the fields "street", "city", "state", "zip":
_jsonb_path_exists
Fetch all orders where the items array contains at least one item with a quantity greater than 5. Uses the
PostgreSQL @? operator with a SQL/JSON path
expression:
_jsonb_path_match
Fetch all products where the nested price value is less than 50. Uses the PostgreSQL @@ operator with a
SQL/JSON path predicate expression: