简体   繁体   中英

Postgres query array elements inside JSON?

I have Postgres 11 table called fb_designs that has a json column with data structured like so:

{
    "listings": [
       {
            "id": "KTyneMdrAhAEKyC9Aylf",
            "active": true
        },
        {
            "id": "ZcjK9M4tuwhWWdK8WcfX",
            "active": false
        }
    ]
}

and a tags column in a character varying[] format as so {dWLaRWChaThFPH6b3BpA,BrYiPaUiou020hsmRugR} . Both lengths are undefined.

What I am trying to do is produce a some queries that will let me say in laymans terms,

show me all results where at all items.listings has an active status and tags contains BrYiPaUiou020hsmRugR

I got this far, however I'm not sure how to add in WHERE uid = "foo" , WHERE tags contains "foo", "bar and WHERE title is like %hoot%

SELECT id, title, tags, selected_preview_image, items
FROM  fb_designs r, json_array_elements(r.items#>'{listings}') obj 
WHERE  obj->>'active' = 'true' 
GROUP BY id

If they are all true, then none of them are false. Sounds like you want to negate the containment operation over false.

select * from fb_designs where 
    not items::jsonb @> '{"listings":[{"active": false}]}'
    and tags && ARRAY['BrYiPaUiou020hsmRugR']::varchar[]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM