在Postgres中查询包含数组元素的JSONB

yjghlzjz  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(150)

我有一个名为settings的表,其中program_id(number), client_id(number), filters(Jsonb)列和filters列包含jsonb数据,格式如下-

{
  "sources": [
    {
      "conditions": [
        {
          "value": [
            {
              "id": 1211,
              "name": "ABM BETA INVITE LIST",
              "isSelected": true
            }
          ],
          "condition": "one of the following"
        }
      ],
      "objectType": "SmartLists",
      "subscriptionId": 1173,
      "integrationType": "mkto"
    }
  ],
  "listType": "All Accounts",
  "programId": 30203,
  "noOfAccounts": null,
  "expiryDuration": 0,
  "subscriptionId": null,
  "updateFrequency": null
}

我现在想从表settingswhere filters.sources[0].integrationType = 'mkto'中检索所有记录。我已经尝试了此查询,但返回的集合函数在WHERE-中不允许

select * from settings where (jsonb_array_elements(filters -> 'sources') ->> 'integrationType' = 'mkto');
zkure5ic

zkure5ic1#

现在,我想从filters.sources[0].integrationType = 'mkto'所在的表设置中检索所有记录。
使用#>>运算符:

SELECT *
FROM   settings
WHERE  filters #>> '{sources, 0, integrationType}' = 'mkto';

fiddle
filters #>> '{sources, 0, integrationType}'与以下公式相同:

filters -> 'sources' -> 0 ->> 'integrationType'
filters['sources'][0]['integrationType'] #>> '{}'  -- for Postgres 14+

但是您真的只想查看第一个数组元素吗?

相关问题