简体   繁体   English

PostgreSQL 对 JSONB 对象数组的查询

[英]PostgreSQL query on a JSONB Array of Objects

I've a PostgreSQL table with this complex structure:我有一个具有这种复杂结构的 PostgreSQL 表:

"item": [{
        "field10",
        "field11",
        "field12": {
            "field20"
        },
        "field13": [{
                "field21": [{
                        "field30"
                    }
                ],
                "field22": [{
                        "field31"
                        "field32": {
                            "field40"
                        }
                    }
                ],
            }
        ]
    }]

And I've to make a query for filter until the field40 many explication than I saw are for objects mor simple.而且我必须对过滤器进行查询,直到field40比我看到的对象更简单的许多解释。 Can you explain me how can I do?你能解释一下我该怎么做吗?

Edit:编辑:

The example what I made i more simplest than reality.我做的例子比现实更简单。 In reality the object is very very long.实际上,object 非常长。 If I try to make a simple example with values, that's like that:如果我尝试用值做一个简单的例子,那就是这样:

{
"uuid": "64fc7a55-be2d-41d8-99c1-d08fa5780731",
"item": [{
            "field10" : 2004,
            "field11" : false,
            "field12": {
                "field20" : "cc092aa1-6465-4526-9292-373344ed3e18",
                "field21" : "AMAZONE"
            },
            "field13": [{
                    "field22": [{
                            "field30" : "ee5bfd21-b2ce-453e-94a2-ef9c87002758",
                            "field31" : "MICROSOFTE"
                        },{
                            "field30" : "2eb39939-08b9-4456-9b5b-eff88f00bacb",
                            "field31" : "GOOGLEE"
                        }
                    ],
                    "field23": [{
                            "field32" : "e70bec99-689a-4c5d-ab81-e7601bc5b435",
                            "field33": {
                                "field40" : "a70ce09e-21ec-46cd-bfe6-591b84c0b328",
                                "field41" : "https://stackoverflow.com/"
                            }
                        },{
                            "field32" : "e70bec99-689a-4c5d-ab81-e7601bc5b435",
                            "field33": {
                                "field40" : "7bfa6882-2e65-4f96-b4a5-6aa0d148a95d",
                                "field41" : "https://gis.stackexchange.com/"
                            }
                        }
                    ],
                }
            ]
        }]
},
{
"uuid": "f131864a-338e-4f2c-91b1-13950958bd62",
"item": [{
            "field10" : 2012,
            "field11" : false,
            "field12": {
                "field20" : "2b8176e4-c858-4159-b279-d852df77c63b",
                "field21" : "PIXABAYE"
            },
            "field13": [{
                    "field22": [{
                            "field30" : "9e70b3d8-8f3f-4ae1-ba64-86efa513bd28",
                            "field31" : "IOBEYAE"
                        },{
                            "field30" : "0150dba2-98de-4143-bd81-329b74874403",
                            "field31" : "PINTERESTE"
                        }
                    ],
                    "field23": [{
                            "field32" : "3ab19111-513a-47db-9fb5-52dc0a1e1c4d",
                            "field33": {
                                "field40" : "7be95726-6698-4bdd-8f6b-2595d1704a2a",
                                "field41" : "https://google.com/"
                            }
                        },{
                            "field32" : "7993aba5-f13f-4998-af7b-9b0b8aec8aa9",
                            "field33": {
                                "field40" : "4740dbcf-84c4-40ff-a633-9207e79cabf4",
                                "field41" : "https://yahoo.com/"
                            }
                        }
                    ],
                }
            ]
        }]
}

In this example, my Postgres table have two columns, one with the primary key uuid and one with the object in jsonb item And what I want, for example is to make a filter on my items where "field41" contains value "gis.stackexchange.com" for et in this example if I search "field41" = "https://yahoo.com/" the tuple with "uuid" = "uuid": "64fc7a55-be2d-41d8-99c1-d08fa5780731" will not be in the result在这个例子中,我的 Postgres 表有两列,一列带有主键uuid ,另一列带有 jsonb中的 object 而我想要的,例如,对我的项目进行过滤,其中“field41”包含值“gis.stackexchange在这个例子中,如果我搜索 "field41" = "https://yahoo.com/" 的 .com" 元组 "uuid" = "uuid": "64fc7a55-be2d-41d8-99c1-d08fa5780731" 将不会结果中

With PostgreSQL v12 and better, you can use the jsonpath language to write such a query:使用 PostgreSQL v12 及更高版本,您可以使用 jsonpath 语言编写这样的查询:

SELECT uuid
FROM jtab
WHERE jsonb_path_exists(
         item,
         '$[*].field13[*].field23[*].field33.field41 ? (@ == "https://yahoo.com/")'
      );

Your question suggests that you also need to accomplish a partial match on the URL in "field41" .您的问题表明您还需要在"field41"的 URL 上完成部分匹配。 This may be accomplished by enumerating each nested array with jsonb_array_elements and then using WHERE... LIKE... to filter records matching the desired domain name:这可以通过使用jsonb_array_elements枚举每个嵌套数组,然后使用WHERE... LIKE...过滤与所需域名匹配的记录来完成:

SELECT
   t.data->>'uuid' as uuid,
   jfield23->'field33'->>'field40' as field40,
   jfield23->'field33'->>'field41' as field41
FROM
   jtest t,
   jsonb_array_elements(t.data->'item') as jitem,
   jsonb_array_elements(jitem->'field13') as jfield13,
   jsonb_array_elements(jfield13->'field23') as jfield23
WHERE jfield23->'field33'->>'field41' LIKE '%' || 'yahoo.com' || '%'

I've included the the relevant field40 and field41 in the SELECT clause for illustration purposes.为了便于说明,我在SELECT子句中包含了相关的field40field41

See the full example on db<>fiddle .请参阅db<>fiddle上的完整示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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