繁体   English   中英

Sequelize/postgres:获取与 object 数组中的值与提供的值匹配的记录

[英]Sequelize/postgres: Get records matching a value inside an array of object with a provided value

我有一个存储 object 数组的 db 列。 一个示例值为:

[{
  "recordId": 11,
  "column": [{
      "name": "some value",
      "id": "1"
    },
    {
      "name": "other value",
      "id": "2"
    }
  ]
}, {
  "recordId": 12,
  "column": [{
      "name": "some value",
      "id": "3"
    },
    {
      "name": "other value",
      "id": "4"
    }
  ]
}]

现在,如果我想通过 sequelize 搜索具有id:1的记录, where子句中应该包含什么,或者是否有其他方法?

您正在寻找 json arrays 至少有一个 object 的行,其id属性值为1 在纯 SQL 中,您可以将exists与取消嵌套数组的相关子查询一起使用:

select t.*
from mytable t
where exists (
    select 1
    from jsonb_array_elements(t.column) as x(obj)
    where (x.obj ->> 'id')::int = 1
)

暂无
暂无

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

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