繁体   English   中英

如何查询 select 所有与 JSONB object 中的对象数组中的值匹配的行?

[英]How do I query select all rows that match a value inside an array of objects inside a JSONB object?

我有一个使用 Supabase 设置的数据库。 数据库的名称是sites 除了其他一些列,我还有一个名为sitedata的 JSONB 列。

下面是sitedata列的内容示例。

{
  "status": true,
  "response": {
    "theme": {
      "desc": "Description",
      "name": "A theme name",
      "author": "An author",
      "version": "2.14.2",
      "isparent": "Yes"
    },
    "plugins": [
      {
        "desc": "AMP for WP - Accelerated Mobile Pages for WordPress",
        "name": "Accelerated Mobile Pages",
        "author": "Ahmed Kaludi, Mohammed Kaludi",
        "status": "Inactive",
        "update": "Yes",
        "version": "1.0.77.42",
        "new_version": "1.0.79"
      },
      {
        "desc": "Customize WordPress with powerful, professional and intuitive fields.",
        "name": "Advanced Custom Fields PRO",
        "author": "Delicious Brains",
        "status": "Active",
        "update": "No",
        "version": "5.12.3",
        "new_version": "-"
      },
      {
        "desc": "Used by millions, Akismet is quite possibly the best way in the world to <strong>protect your blog from spam</strong>. It keeps your site protected even while you sleep. To get started: activate the Akismet plugin and then go to your Akismet Settings page to set up your API key.",
        "name": "Akismet Anti-Spam",
        "author": "Automattic",
        "status": "Inactive",
        "update": "Yes",
        "version": "4.2.2",
        "new_version": "5.0.2"
      }
    ]
  }
}

我需要能够获取在sitedata.response.plugins对象数组中共享键值的每一行。

我已经尝试过人们通过我的谷歌搜索建议的许多事情,但没有任何效果。 我最后尝试的是这个,但它返回一个空数组。

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

这最终是有效的。

let { data, error } = await supabase.from('sites')
.select('*') 
.not('lastupdated', 'is', null)
.contains('sitedata->response->plugins', JSON.stringify([{'name': 'Accelerated Mobile Pages'}]));

暂无
暂无

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

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