如何使用特定值条件查询jsonb中的对象

huangapple go评论65阅读模式
英文:

How to query objects in jsonb with specific value criteria

问题

I'm trying to query a jsonb column in Postgres. I've created it from a nested java map, it isn't in array format, and can't work out if that is the thing that is causing me to have issues with my query.

我正在尝试查询Postgres中的jsonb列。我将其从嵌套的Java映射创建,它不是数组格式,并且无法确定是否是导致我的查询出现问题的原因。

I want to query the jsonb column for items containing an object with "type": "Unknown" and also have a time that is greater than 100.

我想查询jsonb列中包含具有"type": "Unknown"并且时间大于100的项目。

id|uid                                 |process_stat_json                                                                                                                                                                                                                                                           |
---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
 1|9cf237e8-0c73-4c4d-a60c-30b244789e67|{"Type A": {"time": 500, "complete": 100.0, "type": "Unknown"}, "Type B": {"time": 35, "complete": 100.0, "type": "Parallel"}, "Type C": {"time": 50, "complete": 100.0, "type": "Serial"}}
 2|07ac957f-c9e4-460e-b75f-79c0d9c550d4|{"Type A": {"time": 55, "complete": 100.0, "type": "Parallel"}, "Type C": {"time": 20, "complete": 100.0, "type": "Serial"}}
 3|8e8ef827-fff1-4ea1-a990-206b557ef27a|{"Type C": {"time": 100, "complete": 100.0, "type": "Parallel"}, "Type A": {"time": 55, "complete": 100.0, "type": "Unknown"}, "Type D": {"time": 70, "complete": 100.0, "type": "Serial"}}
 4|5279e6ae-f2e8-4fae-8d0b-9297b5e2936a|{"Type A": {"time": 200, "complete": 100.0, "type": "Serial"}, "Type D": {"time": 20, "complete": 100.0, "type": "Unknown"}, "Type C": {"time": 80, "complete": 100.0, "type": "Serial"}}

So for example the query would return id:1

因此,例如查询将返回id:1

I've started creating a query to search for items with the type "Unknown"

我已经开始创建一个查询来搜索类型为"Unknown"的项目

select p.*
from
  process p
where
  p.process_stat_json  @> '{"type":"Unknown"}';

But this isn't even returning any items at all, let alone allowing me to query them, so unsure how to proceed from here.

但这甚至没有返回任何项目,更不用说允许我查询它们了,所以不确定该如何继续。

英文:

I'm trying to query a jsonb column in Postgres. I've created it from a nested java map, it isn't in array format, and can't work out if that is the thing that is causing me to have issues with my query.

I want to query the jsonb column for items containing an object with '"type": "Unknown" and also have a time that is greater than 100.

id|uid                                 |process_stat_json                                                                                                                                                                                                                                                           |
---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1|9cf237e8-0c73-4c4d-a60c-30b244789e67|{"Type A": {"time": 500, "complete": 100.0, "type": "Unknown"}, "Type B": {"time": 35, "complete": 100.0, "type": "Parallel"}, "Type C": {"time": 50, "complete": 100.0, "type": "Serial"}}
2|07ac957f-c9e4-460e-b75f-79c0d9c550d4|{"Type A": {"time": 55, "complete": 100.0, "type": "Parallel"}, "Type C": {"time": 20, "complete": 100.0, "type": "Serial"}}
3|8e8ef827-fff1-4ea1-a990-206b557ef27a|{"Type C": {"time": 100, "complete": 100.0, "type": "Parallel"}, "Type A": {"time": 55, "complete": 100.0, "type": "Unknown"}, "Type D": {"time": 70, "complete": 100.0, "type": "Serial"}}
4|5279e6ae-f2e8-4fae-8d0b-9297b5e2936a|{"Type A": {"time": 200, "complete": 100.0, "type": "Serial"}, "Type D": {"time": 20, "complete": 100.0, "type": "Unknown"}, "Type C": {"time": 80, "complete": 100.0, "type": "Serial"}}

So for example the query would return id:1

I've started creating a query to search for items with the type "Unknown"

select p.*
from
process p
where
p.process_stat_json  @> '{"type":"Unknown"}';

But this isn't even returning any items at all, let alone allowing me to query them, so unsure how to proceed from here.

答案1

得分: 1

Your first request can be solved elegantly with the JSON path operator @> - in Postgres 12 or later:

SELECT p.*
FROM   process p
WHERE  p.process_stat_json @? ''$.* ? (@.type == "Unknown") ? (@.time >= 100)'';

fiddle

Related:

英文:

Your first request can be solved elegantly with the JSON path operator @> - in Postgres 12 or later:

SELECT p.*
FROM   process p
WHERE  p.process_stat_json @? '$.* ? (@.type == "Unknown") ? (@.time >= 100)';

fiddle

Related:

huangapple
  • 本文由 发表于 2023年7月13日 14:07:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676379.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定