最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

postgresql - Find row where an exact JSON key value pair exists - Stack Overflow

programmeradmin2浏览0评论

I have a table in PostgreSQL that contains two columns, one being a bigint primary key and the other contains JSONB with a mix of arrays and simple key value pairs.

If I wanted to return the rows in the table where the JSON data in the second column contains a certain exact key value pair, what would the query need to look like? The second column is a JSONB column.

I have been reviewing the PostgreSQL documentation, but I cannot find the operators I would need to make this happen.

I have a table in PostgreSQL that contains two columns, one being a bigint primary key and the other contains JSONB with a mix of arrays and simple key value pairs.

If I wanted to return the rows in the table where the JSON data in the second column contains a certain exact key value pair, what would the query need to look like? The second column is a JSONB column.

I have been reviewing the PostgreSQL documentation, but I cannot find the operators I would need to make this happen.

Share Improve this question edited Feb 3 at 5:40 Ken White 126k15 gold badges236 silver badges466 bronze badges asked Feb 3 at 3:52 ma89581ma89581 1
Add a comment  | 

1 Answer 1

Reset to default 1

Your question is not totally clear on that, but if the key-value pair you are looking for is not at the top level of the JSON structure, your best option would be to use a JSONPATH search.

This example searches for a { "key": "value" } pair anywhere nested in a JSON structure:

SELECT JSONB '{
                "x": [
                       { "key": "value" },
                       { "key": "othervalue" }
                     ]
              }'
       @@ 'strict $.**.key == "value"';
发布评论

评论列表(0)

  1. 暂无评论