37

I'm experimenting with postgres jsonb column types, and so far so good. One common query I'm using is like this:

select count(*) from jsonbtest WHERE attributes @> '{"City":"Mesa"}';

How do I reverse that? Is there a different operator or is it simply used as

select count(*) from jsonbtest WHERE NOT attributes @> '{"City":"Mesa"}';
3
  • 7
    No, there is no dedicated operator for that. What's wrong with NOT?
    – redneb
    Sep 28, 2016 at 0:06
  • @redneb The problem is that NOT simply doesn't work. Using the attributes->>'City' <> 'Mesa' formulation also doesn't work.
    – eykanal
    Mar 30, 2017 at 2:09
  • 2
    @eykanal NOT works pretty well.
    – pozs
    Apr 28, 2017 at 11:50

4 Answers 4

31

Two way, you can test any json(b) value

  • the ->> operator extract value as text. But this operation slow, if you will use the value only test
  • the @> operator test any json(b) contain any json(b). This is a quick but you are not tested NOT option.

Simply and quick way:

NOT (attribute @> '{"City":"Mesa"}'::jsonb)

I've change attribute->>'City' <> 'Mesa' to NOT (attribute @> '{"City":"Mesa"}'::jsonb) and my ~2.000.000 rows query result time changed 45secs to 25secs.

4
  • The frustrating thing is that if you've added a nice USING GIN(attribute) index, negating the @> contains operator suddenly stops using that index.
    – nessur
    Feb 6, 2020 at 18:38
  • 1
    NOT doesn't covers situation when you have attribute but this attribute equals NULL Mar 1, 2021 at 8:25
  • @DmitryChirkin simpe test. null valued object: SELECT NOT('{"City":null}'::jsonb @> '{"City":"Mesa"}'::jsonb); returns as true alternatively test. not contain attribute: SELECT NOT('{"Another Attribute":"Mesa"}'::jsonb @> '{"City":"Mesa"}'::jsonb) returns true i think there is no problem Jan 27 at 6:45
  • @nessur You can create the GIN index with jsonb_ops. e.g: CREATE INDEX idx_my_index_jsonb_data_with_ops ON my_table USING GIN (attribut jsonb_ops); You can use this link for options: postgresql.org/docs/current/gin-builtin-opclasses.html Jan 27 at 6:50
12

This can be achieved with several conditions. It's not elegant but I didn't find another way to do so.

So, first, get every row which simple don't have 'City' attribute and then add 'OR' condition to check for correct field value.

select count(*) from jsonbtest where 
  NOT(attributes ? 'City') 
  OR (attributes ? 'City') is NULL -- this is required if attributes can be null
  OR (attributes->>'City' != 'Mesa')) 
1
  • 1
    This helped me a lot... I couldn't find anyting about on this use case on the PgSQL site. Thanks!
    – A-Diddy
    Feb 26, 2021 at 22:29
2

---contains specific key (City), does not contain value ('"Mesa"').

SELECT count(*) FROM jsonbtest  WHERE NOT (attributes  -> 'City'  @> '"Mesa"');

---does not contain specific key value pair: {"City":"Mesa"}

SELECT count(*) FROM jsonbtest  WHERE  NOT (attributes  @> '{"City":"Mesa"}')

Since attributes key can be others except City. So these two queries are different!

-3

You can use the operator <@ this will search where 'City' is not 'Mesa'

select count(*) from jsonbtest WHERE attributes <@ '{"City":"Mesa"}';
1
  • That would be the same as select count(*) from jsonbtest WHERE '{"City":"Mesa"}' @> attributes;, checking if {"City": "Mesa"} contains attributes which is not the same.
    – drice304
    Dec 17, 2019 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.