我可以在 postgres 的 JSON 中索引和搜索嵌套对象键吗?

Can I index and search nested object keys in JSON on postgres?

如果我有一个名为 configurations 的 table,其中行包含一个名为 datajsonb 列,其值类似于以下内容:

{
    "US": {
        "1234": {
            "id": "ABCD"
        }
    },
    "CA": {
        "5678": {
            "id": "WXYZ"
        }
    }
}

我希望能够编写类似于以下内容的查询:

select * from configurations where data->'$.*.*.id' = 'WXYZ'

(请注意:我知道上面的SQL不正确,请视为伪造。)

问题:

你可以把你的伪代码变成真正的jsonpath代码:

select * from configurations where data @@ '$.*.*.id == "WXYZ"'

这可以在“数据”上使用默认的杜松子酒索引:

create index on configurations using gin (data);