我需要在 postgres 中查詢 jsonb 物件陣列中的元素。
"data": {
"text1": [
{
"lang": "de",
"property1": "abc",
"property2": "def"
},
{
"lang": "en",
"property1": "ghi",
"property2": "jkl"
}
],
"text2": [
{
"lang": "de",
"property1": "mno",
"property2": "pqr"
},
{
"lang": "en",
"property1": "stu",
"property2": "vwx"
}
]
}
是否可以在 jsonpath 樣式的 where 子句中選擇欄位:
$.data.text1[?(@.lang=="de")]
例如:
select id, json from xyz where json->'data'->'text1'->[?(@.lang=="de")]->property1 is not null
應該可以查詢多個屬性,例如:
select id, json from xyz where json->'data'->'text1'->[?(@.lang=="de")]->property1 is not null and json->'data'->'text2'->[?(@.lang=="de")]->property2 = "pqr"
uj5u.com熱心網友回復:
可以使用 JSON 路徑運算式,例如:
select *
from the_table
where the_column @@ '$.data[*].** ? (@.lang == "de").property2 == "pqr"'
或者
select *
from the_table
where the_column @@ '$.data[*].** ? (@.lang == "de").property1 <> null'
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/511360.html
上一篇:添加布爾型別的jsonb屬性
