简体   繁体   English

在 Postgres 中查询具有数组元素的 JSONB

[英]Querying JSONB having array elements in Postgres

I have a table named settings with the columns program_id(number), client_id(number), filters(Jsonb) and filters column has jsonb data in such format-我有一个名为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
}

I now want to retrieve all the records from table settings where filters.sources[0].integrationType = 'mkto' .我现在想从filters.sources[0].integrationType = 'mkto'设置中检索所有记录。 I have tried this query but gives me error of set-returning functions are not allowed in WHERE-我试过这个查询但是给我错误的设置返回函数不允许在 WHERE-

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

I now want to retrieve all the records from table settings where filters.sources[0].integrationType = 'mkto' .我现在想从filters.sources[0].integrationType = 'mkto'表设置中检索所有记录。

Using the #>> operator :使用#>>运算符

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

fiddle小提琴

filters #>> '{sources, 0, integrationType}' is the same as: filters #>> '{sources, 0, integrationType}'与:

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

But do you really only want to look at the first array element?但是你真的只想看第一个数组元素吗?

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

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