在 Postgresql JSON 数组字段中查找序列

发布于 2025-01-18 14:33:15 字数 810 浏览 2 评论 0原文

我有 PostgreSQL 数据库,其 JSONB 类型字段名为“元数据”,该字段具有以下内容结构:

row1: {   
"name": "test",   
"description": "test",   
"attributes" : [      {        "type": "level" ,       "level": 1      },      {        "type": "name"    ,    "name": "Fish"      }   ] 
}

row2: {   
"name": "test2",   
"description": "test2",   
"attributes" : [      {        "type": "level",        "level": 4      },      {        "type": "name"  ,      "name": "Cat"      }   ] 
}

row3: {   
"name": "test4",   
"description": "test4",   
"attributes" : [      {        "type": "level",        "level": 7      },      {        "type": "name"    ,    "name": "Dog"      }   ] }

我尝试使用 Sequelize 在 [1, 5] 之间按 attribute.level 进行筛选,发出 findAll 查询请求,但没有得到一些结果。如何在json数组之间查找?

I have PostgreSQL database with JSONB type field named "metadata" that has following content structure:

row1: {   
"name": "test",   
"description": "test",   
"attributes" : [      {        "type": "level" ,       "level": 1      },      {        "type": "name"    ,    "name": "Fish"      }   ] 
}

row2: {   
"name": "test2",   
"description": "test2",   
"attributes" : [      {        "type": "level",        "level": 4      },      {        "type": "name"  ,      "name": "Cat"      }   ] 
}

row3: {   
"name": "test4",   
"description": "test4",   
"attributes" : [      {        "type": "level",        "level": 7      },      {        "type": "name"    ,    "name": "Dog"      }   ] }

I try to make findAll query request with filter by attributes.level between [1, 5] using Sequelize, but don't get some results. how find between json array?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙气飘飘 2025-01-25 14:33:15

这里它是一个参数化查询。 :lo:hi 是参数。 the_table CTE 代表真实表,以供说明。

with the_table(r, metadata) as 
(
 values
 ('row1', '{"name": "test",  "description": "test",  "attributes" : [ { "type": "level" , "level": 1}, { "type": "name" , "name": "Fish" }]}'::jsonb),
 ('row2', '{"name": "test2", "description": "test2", "attributes" : [ { "type": "level", "level": 4 }, { "type": "name" , "name": "Cat" }]}'),
 ('row3', '{"name": "test4", "description": "test4", "attributes" : [ { "type": "level", "level": 7 }, { "type": "name" , "name": "Dog" }]}')
)
select * from the_table
where exists 
(
  select from jsonb_array_elements(metadata -> 'attributes') e 
  where (e ->> 'level')::numeric between :lo and :hi
);

Here it is as a parameterized query. :lo and :hi are parameters. the_table CTE stands for the real table as an illustration.

with the_table(r, metadata) as 
(
 values
 ('row1', '{"name": "test",  "description": "test",  "attributes" : [ { "type": "level" , "level": 1}, { "type": "name" , "name": "Fish" }]}'::jsonb),
 ('row2', '{"name": "test2", "description": "test2", "attributes" : [ { "type": "level", "level": 4 }, { "type": "name" , "name": "Cat" }]}'),
 ('row3', '{"name": "test4", "description": "test4", "attributes" : [ { "type": "level", "level": 7 }, { "type": "name" , "name": "Dog" }]}')
)
select * from the_table
where exists 
(
  select from jsonb_array_elements(metadata -> 'attributes') e 
  where (e ->> 'level')::numeric between :lo and :hi
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文