Search code examples
postgresqlhstorenegation

Find all where hstore does not contain any specified keys


In the hstore docs there is a documented operator to check if the hstore contains all specified keys:

Operator: hstore ?& text[]

Description: does hstore contain all specified keys?

Example: 'a=>1,b=>2'::hstore ?& ARRAY['a','b']

Result: => t

My use case is the exact opposite: I want to check if the hstore does not contain all specified keys.

So 'a=>1,b=>2'::hstore SOME_OPERATOR ARRAY['b','c'] should return false since 'a=>1,b=>2' contains b.

I tried !?& but to no surprise, that didn't work. Is there such an operator? Or a method to negate the documented one?


Solution

  • Negate the condition and change the operator to ?| so that it does not match any keys from the array:

    select *
    from some_table
    where not (some_column ?| ARRAY['b','c'])