Postgres jsonb顶层和子层查询

时间:2019-06-12 14:21:37

标签: postgresql

我有一个看起来像这样的JSON对象:

{
    "all_books": [
        {
            "book_id": "dcbdfc3b-6b4f-44b6-9309-2a855f840c66",
            "book_name": "book14",
            "book_type": "public"
        },
        {
            "book_id": "d367ffba-b2e1-411f-9250-6e0a51bdaa9b",
            "book_name": "book19",
            "attributes": {
                "attribute_json": "{\"number\": \"9\"}"
            },
            "book_type": "private"
        },
        {
            "book_id": "e39a2006-4286-4f9f-81a5-a513a34b933c",
            "book_name": "book16",
            "attributes": {
                "attribute_json": "{\"number\": \"25\"}"
            },
            "book_type": "private"
        }
    ]
}

我需要查询像这样的大数据表:

  • 请给我所有公开且已输入book_id的图书​​或
  • 请给我所有私人书籍,已输入book_id并具有等于/包含已输入attribute_json的attribute_json

这是我到目前为止的查询

select *
from test_data_json
where 
    book_json -> 'all_books' @> '[{"book_id":"d367ffba-b2e1-411f-9250-6e0a51bdaa9b", "book_type":"public"}]' OR 
    (
        book_json -> 'all_books' @> '[{"book_id":"d367ffba-b2e1-411f-9250-6e0a51bdaa9b", "book_type":"private"}]' -- AND
        -- determine if the attribute matches --> all_books/attributes/attribute_json
    )

0 个答案:

没有答案