You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PostgreSQL中使用JSONPath检测属性不存在?

查询不包含指定属性的JSON对象

我可以通过以下语句查询包含特定值属性的JSON对象:

jsonb_path_query(p.blah::jsonb, '$[*] ? (@.fruit == "banana") ')

但想要查询不包含指定属性的JSON对象时,之前的写法会报错:

  • 尝试jsonb_path_query(p.blah::jsonb, '$[*] ? (!(@ ? fruit))'),提示unexpected IDENT_P
  • 尝试jsonb_path_query(p.blah::jsonb, '$[*] ? (!(@ ? "fruit"))'),提示unexpected STRING_P

正确写法

可以使用PostgreSQL JSON Path语法的两种方式实现需求:

方法1:用exists()函数取反

jsonb_path_query(p.blah::jsonb, '$[*] ? (!exists(@.fruit))')

方法2:用!?操作符

jsonb_path_query(p.blah::jsonb, '$[*] ? (@ !? "fruit")')

这两种写法都能正确筛选出数组中不包含fruit属性的JSON对象。

内容的提问来源于stack exchange,提问作者evilfred

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 14:54:19