I've been trying to understand the difference between the PostgreSQL @?
and @@
JSONB operators. Why do these return different results?
david=# SELECT '{ "email": { "main": "[email protected]" } }' @? '$ ?(@.email.main == "[email protected]")';
?column?
----------
t
david=# SELECT '{ "email": { "main": "[email protected]" } }' @@ '$ ?(@.email.main == "[email protected]")';
?column?
----------
f
I thought maybe they're equivalent to the jsonb_path_exists()
and jsonb_path_match()
functions:
david=# SELECT jsonb_path_exists('{ "email": { "main": "[email protected]" } }', '$ ?(@.email.main == "[email protected]")');
jsonb_path_exists
-------------------
t
david=# SELECT jsonb_path_match('{ "email": { "main": "[email protected]" } }', '$ ?(@.email.main == "[email protected]")');
ERROR: single boolean result is expected
That's a bit more informative, and jsonb_path_match()
example in the docs show the use of exists()
, and that does seem to work:
david=# SELECT jsonb_path_match('{ "email": { "main": "[email protected]" } }', 'exists($ ?(@.email.main == "[email protected]"))');
jsonb_path_match
------------------
t
But the same is not true of @@
:
david=# SELECT '{ "email": { "main": "[email protected]" } }' @@ 'exists($ ?(@.email.main == "[email protected]"))';
?column?
----------
f
So color me confused. I do not understand the differences here.