简体   繁体   English

PostgreSQL JsonB根据对象属性查询JSON数组中的对象

[英]PostgreSQL JsonB query for object in JSON array based on object attributes

So I have seen a few other responses to this question on StackOverflow already, but none of them have worked for me.所以我已经在 StackOverflow 上看到了对这个问题的其他一些回复,但没有一个对我有用。

{
    "data": {
        "list": [
            {"id": "2ac5bf6f-bc4a-49e8-8f9f-bc518a839981", "type": "type1"},
            {"id": "d15ac090-11ce-4c0c-a05d-d4238f01e8b0", "type": "type3"},
            {"id": "b98958fa-87c4-4dcc-aa84-beaf2b32c5c0", "type": "type1"},
            {"id": "854f4d2a-f37c-42cb-9a1f-17a15454a314", "type": "type2"},
            {"id": "555816da-4547-4a82-9e7e-1e92515bd82b", "type": "type2"},
            {"id": "0f7f4ced-61c2-45da-b15c-0e12058f66a7", "type": "type4"}

        ]
    }
}

This Json is stored in a field called "questions", now I want to query this table for an object with a certain id in the list.这个 Json 存储在一个名为“questions”的字段中,现在我想在这个表中查询列表中具有某个 id 的对象。 So say I have the id 555816da-4547-4a82-9e7e-1e92515bd82b , I would like to return所以说我有 id 555816da-4547-4a82-9e7e-1e92515bd82b ,我想返回

{"id": "555816da-4547-4a82-9e7e-1e92515bd82b", "type": "type2"} 

The solutions to this I have seen on the internet (primarily here), that have not worked are here:我在互联网上看到的(主要是这里)没有奏效的解决方案在这里:

SELECT questions->'data'->'list' 
FROM assignments 
WHERE id='81asd6230-126d-4bc8-9745-c4333338115c' 
AND questions->'data'->'list' @> '[{"id":"854f4d2a-f37c-42cb-9a1f-17a15454a314"}]';

I have seen this solution on multiple different responses but it doesn't narrow down the array at all, it returns the full thing every time.我已经在多个不同的响应中看到了这个解决方案,但它根本没有缩小数组的范围,它每次都返回完整的东西。 The first id in the where clause is the id of the specific assignment object that I want, its mostly irrelevant here. where 子句中的第一个 id 是我想要的特定分配对象的 id,它在这里几乎无关紧要。

SELECT questions->'data'->'list' 
FROM assignments 
WHERE id='81asd6230-126d-4bc8-9745-c4333338115c' 
AND questions->'data'->'list' @> '[{"id":"854f4d2a-f37c-42cb-9a1f-17a15454a314"}]';

This returns nothing.这不返回任何内容。

Does anyone have an idea on how to do this easily?有谁知道如何轻松做到这一点?

You can use function jsonb_array_elements(jsonb) to select all elements of a json array:您可以使用函数jsonb_array_elements(jsonb)选择 json 数组的所有元素:

select jsonb_array_elements(questions->'data'->'list') elem
from assignments
where id='81asd6230-126d-4bc8-9745-c4333338115c'

                              elem
-----------------------------------------------------------------
 {"id": "2ac5bf6f-bc4a-49e8-8f9f-bc518a839981", "type": "type1"}
 {"id": "d15ac090-11ce-4c0c-a05d-d4238f01e8b0", "type": "type3"}
 {"id": "b98958fa-87c4-4dcc-aa84-beaf2b32c5c0", "type": "type1"}
 {"id": "854f4d2a-f37c-42cb-9a1f-17a15454a314", "type": "type2"}
 {"id": "555816da-4547-4a82-9e7e-1e92515bd82b", "type": "type2"}
 {"id": "0f7f4ced-61c2-45da-b15c-0e12058f66a7", "type": "type4"}
(6 rows)

Use the above query if you want to select an element with specific id :如果要选择具有特定id的元素,请使用上述查询:

select elem
from (
    select jsonb_array_elements(questions->'data'->'list') elem
    from assignments
    where id='81asd6230-126d-4bc8-9745-c4333338115c'
    ) sub
where elem->>'id' = '854f4d2a-f37c-42cb-9a1f-17a15454a314'

                              elem
-----------------------------------------------------------------
 {"id": "854f4d2a-f37c-42cb-9a1f-17a15454a314", "type": "type2"}
(1 row)

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

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