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

如何在Supabase中查询JSONB字段内数组对象匹配指定值的所有行?

解决Supabase JSONB数组对象匹配查询问题

你的查询返回空数组的核心问题是错误混用了PostgreSQL JSON操作符语法和Supabase方法的参数格式,同时没有正确构造匹配JSONB字段的嵌套结构。

正确查询写法

以下两种方式均可实现需求,按需选择:

方式一:用filter结合PostgreSQL @>操作符

let { data, error } = await supabase.from('sites')
  .select('*')
  .filter('sitedata -> \'response\' -> \'plugins\'', '@>', '[{"name": "Accelerated Mobile Pages"}]');

方式二:用where传递完整JSON匹配结构

let { data, error } = await supabase.from('sites')
  .select('*')
  .where('sitedata @> ?', JSON.stringify({
    response: {
      plugins: [{ name: "Accelerated Mobile Pages" }]
    }
  }));

原理说明

PostgreSQL的@>操作符用于检查JSONB对象是否包含指定子结构:对于数组类型的JSONB字段,只要数组中存在至少一个元素匹配指定的键值对(此处为name: "Accelerated Mobile Pages"),就会返回对应的行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 06:20:27