简体   繁体   中英

Querying against an inner field in postgresql jsonb column

I have a jsonb column, which has a structure like this:

{
    .....other fields,
    "a" : {
        "b" : {
            "c" : "some value",
            ....other fields
        }
        .....other fields
    },
    ...other fields
}

I can have query like this, properties being column name in table MyTable:

SELECT * from
  MyTable t
WHERE t.properties @> '{"a":{"b":{"c": "some value"}}}';

But it is possible that fields before "c" are different,ie we can have:

{
        .....other fields,
        "m" : {
            "n" : {
                "c" : "some value",
                ....other fields
            }
            .....other fields
        },
        ...other fields
    }

How to modify my query for such a scenario?

If the nesting is always at the same level you can use a JSON/Path expression:

select *
from the_table
where properties @@ '$.*.*.c == "some value"'

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