Postson jsonb 'NOT contains' statement

I am experimenting with postgres jsonb column types, and so far so good. One general query that I use is as follows:

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

How do I cancel this? Is there another statement or is it just used as

 select count(*) from jsonbtest WHERE NOT attributes @> '{"City":"Mesa"}'; 
+5
source share
2 answers

This can be achieved with several conditions. This is not elegant, but I have not found another way to do this.

So, first enter each line that does not have the β€œCity” attribute, and then add the β€œOR” condition to check the 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
source

You can use the operator <@ , which will search where "City" is not "Mesa"

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

Source: https://habr.com/ru/post/1257361/


All Articles