我将数据存储为名为"data"的列中的jsonb:
{'people': [{"name": "Bob", "Occupation": "janitor"}, {"name": "Susan", "Occupation", "CEO"}]}
Run Code Online (Sandbox Code Playgroud)
我可以通过以下方式查询:
SELECT mydata.pk FROM mydata, jsonb_array_elements(mydata.data->'people') AS a WHERE (a->>'name') = 'bob'
Run Code Online (Sandbox Code Playgroud)
为什么我不能用"a"代替jsonb_array_elements(...)?:
SELECT mydata.pk FROM mydata WHERE (jsonb_array_elements(mydata.data->'people')->>'name') = 'bob'
Run Code Online (Sandbox Code Playgroud)
相反,我得到以下内容:
ERROR: argument of WHERE must not return a set
Run Code Online (Sandbox Code Playgroud) 我JSONB在PostgreSQL中有一个对象:
'{"cars": ["bmw", "mercedes", "pinto"], "user_name": "ed"}'
Run Code Online (Sandbox Code Playgroud)
我试图在a的子句中使用其中"cars"数组的值:WHERESELECT
SELECT car_id FROM cars WHERE car_type IN ('bmw', 'mercedes', 'pinto');
Run Code Online (Sandbox Code Playgroud)
这将正确返回值1,2和3 - 请参阅本文底部的表格设置.
目前,在我的功能中我这样做:
(1) Extract the "cars" array into a variable `v_car_results`.
(2) Use that variable in the `WHERE` clause.
Pseudo code:
DECLARE v_car_results TEXT
BEGIN
v_car_results = '{"cars": ["bmw", "mercedes", "pinto"], "user_name": "ed"}'::json#>>'{cars}';
-- this returns 'bmw', 'mercedes', 'pinto'
SELECT car_id FROM cars WHERE car_type IN ( v_car_results );
END
Run Code Online (Sandbox Code Playgroud)
但是,该SELECT语句不返回任何行.我知道它正在阅读这3种车型作为单一 …
我将以下postgresql行作为JSONB行:
{age:26}
Run Code Online (Sandbox Code Playgroud)
我想替换它,以便我看起来像这样:
{age: 30, city: "new york city"}
Run Code Online (Sandbox Code Playgroud)
我怎么能在postgressql中做到这一点?有人提到使用jsonb_set(),但我没有看到在一个查询中更新多个键的任何示例.
我正在尝试使用 Postgres 中的 jsonb 解决问题。这里有很多问题,我想做的是:
SELECT table.column->>'key_1' as a FROM "table"
Run Code Online (Sandbox Code Playgroud)
我尝试使用 -> 以及一些括号组合,但我总是在 a.
所以我尝试先获取所有密钥,看看它是否甚至识别 jsonb 。
SELECT jsonb_object_keys(table.column) as a FROM "table"
Run Code Online (Sandbox Code Playgroud)
这引发了一个错误:
cannot call jsonb_object_keys on a scalar
Run Code Online (Sandbox Code Playgroud)
所以,要检查列类型(我创建的,所以我知道它是 jsonb,但无论如何)
SELECT pg_typeof(column) as a FROM "table" ORDER BY "table"."id" ASC LIMIT 1
Run Code Online (Sandbox Code Playgroud)
这在结果中正确地给了我“jsonb”。
中的值column类似于{"key_1":"New York","key_2":"Value of key","key_3":"United States"}
所以,我真的很困惑这里到底发生了什么,为什么将我的 json 数据称为标量?它实际上意味着什么以及如何解决这个问题?
在这方面的任何帮助都将非常有帮助。
PS:我正在使用 rails,将此作为该问题的一般问题发布。任何 Rails 特定的解决方案也可以工作。
我在 postgres 表中有一个 jsonb 列,称为主题,它是字符串的文字数组。示例记录可能具有以下主题值:
['healthcare', 'fitness', 'gains', 'doyouevenliftbro']
我需要在许多匹配的记录中找到这个记录 %lift%
我在 stackoverflow 和 pg 文档上找到的所有其他查询都可以doyouevenliftbro作为一个完整的字符串进行匹配,或者可以进行正则表达式匹配(如果它是从json_to_recordset()或其他转换而来的),但随后它们继续引用 JSON 中的属性,这没有。希望它只是逃避我的简单语法。谢谢!
device_id | device
-----------------------------
9809 | { "name" : "printer", "tags" : [] }
9810 | { "name" : "phone", "tags" : [{"count": 2, "price" : 77}, {"count": 3, "price" : 37} ] }
Run Code Online (Sandbox Code Playgroud)
对于包含数组“标签”的 jsonb 列“设备”上的以下 postgres SQL 查询:
SELECT t.device_id, elem->>'count', elem->>'price'
FROM tbl t, json_array_elements(t.device->'tags') elem
where t.device_id = 9809
Run Code Online (Sandbox Code Playgroud)
device_id 是主键。
我有两个问题不知道如何解决:
我一定是遗漏了一些东西......似乎JSONB_SET()不像宣传的那样工作?
SELECT JSONB_SET(
'{"k1": {"value": "v1"}}',
'{k2,value}',
'"v2"',
TRUE
);
Run Code Online (Sandbox Code Playgroud)
结果是:
----+------------------------
| jsonb_set
| jsonb
----+------------------------
1 | {"k1": {"value": "v1"}}
----+------------------------
Run Code Online (Sandbox Code Playgroud)
我期待{"k1": {"value": "v1"}, "k2": {"value": "v2"}}
我也尝试FALSE作为第四个参数,以防它被逆转或其他什么。
我正在使用 PostgreSQL 9.6.4
我的查询如下
SELECT w.payload,
Count('payload') OVER () AS ROWCOUNT
FROM wholesale_confirmation.wholesale_order_confirmation w
WHERE w.delivery_date = COALESCE(NULL, w.delivery_date)
AND w.ship_to_location_id = COALESCE(NULL, w.ship_to_location_id)
AND w.order_raised_date = COALESCE(NULL, w.order_raised_date)
AND w.ship_from_location_id = COALESCE(NULL, w.ship_from_location_id)
LIMIT 10
OFFSET 0;
Run Code Online (Sandbox Code Playgroud)
这给了我这样的结果:
我想要{"payload:"[payload1,payload2,payload3],"rowcount":n}。
Postgres 10.3版,payload数据类型为jsonb
我在 springboot(2.1)+postgres(10.5)+hibernate(5.3.7) 中使用 jsonb。
以下是文件中的更改:
....
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-52</artifactId>
<version>2.3.5</version>
</dependency>
....
``
@Entity(name = "Event")
@Table(name = "event")
@TypeDefs({
@TypeDef(name = "string-array", typeClass = StringArrayType.class),
@TypeDef(name = "int-array", typeClass = IntArrayType.class),
@TypeDef(name = "json", typeClass = JsonStringType.class),
@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class),
@TypeDef(name = "jsonb-node", typeClass = JsonNodeBinaryType.class),
@TypeDef(name = "json-node", typeClass = JsonNodeStringType.class),
})
public class Event {
@Type(type = "jsonb")
@Column(columnDefinition = "jsonb")
private List<Location> alternativeLocations = new ArrayList<Location>();
//Getters and setters …Run Code Online (Sandbox Code Playgroud) 我尝试设置 GIN 索引,但我认为在运行请求时没有使用我的索引,无论是使用运算符还是函数。
在我们的表中,我们有一个json_aip包含 Json的 JSONB 字段 ( ),如下所示:
{
"properties": {
"pdi": {
"contextInformation": {
"tags": ["SOME_TAG"]
},
},
}
Run Code Online (Sandbox Code Playgroud)
表创建:
create table t_aip (
json_aip jsonb,
[...]
);
CREATE INDEX idx_aip_tags
ON t_aip
USING gin ((json_aip -> 'properties' -> 'pdi' -> 'contextInformation' -> 'tags'));
Run Code Online (Sandbox Code Playgroud)
我们不能?|像使用 JDBC 一样使用运算符。但是有传言说当我运行这种类型的查询时我应该看到我的索引。
EXPLAIN ANALYZE SELECT count(*)
FROM storage.t_aip
WHERE json_aip#>'{properties,pdi,contextInformation,tags}' ?| array['SOME_TAG']
Run Code Online (Sandbox Code Playgroud)
结果:
Aggregate
(cost=27052.16..27052.17 rows=1 width=8) (actual time=488.085..488.087 rows=1 loops=1)
-> Seq Scan on t_aip (cost=0.00..27052.06 …Run Code Online (Sandbox Code Playgroud) jsonb ×10
postgresql ×10
sql ×3
arrays ×2
json ×2
indexing ×1
java ×1
pagination ×1
psycopg2 ×1
spring-boot ×1
sql-limit ×1
where ×1