标签: postgresql-json

PostgreSQL引入的JSONB解释

PostgreSQL刚刚推出了JSONB,它已经在黑客新闻上发展趋势.如果有人能够解释它与之前在PostgreSQL中出现的Hstore和JSON有何不同,那就太好了.它的优点和局限是什么?何时有人考虑使用它?

postgresql json nosql postgresql-json jsonb

304
推荐指数
9
解决办法
11万
查看次数

如何修改新PostgreSQL JSON数据类型中的字段?

使用postgresql 9.3我可以选择JSON数据类型的特定字段,但是如何使用UPDATE修改它们?我在postgresql文档中或在线任何地方都找不到任何这样的例子.我试过了明显的事:

postgres=# create table test (data json);
CREATE TABLE
postgres=# insert into test (data) values ('{"a":1,"b":2}');
INSERT 0 1
postgres=# select data->'a' from test where data->>'b' = '2';
 ?column?
----------
 1
(1 row)
postgres=# update test set data->'a' = to_json(5) where data->>'b' = '2';
ERROR:  syntax error at or near "->"
LINE 1: update test set data->'a' = to_json(5) where data->>'b' = '2...
Run Code Online (Sandbox Code Playgroud)

postgresql json postgresql-9.3 postgresql-json

199
推荐指数
10
解决办法
18万
查看次数

PostgreSQL:从JSON列中删除属性

我需要从json类型列中删除一些属性.

桌子:

CREATE TABLE my_table( id VARCHAR(80), data json);
INSERT INTO my_table (id, data) VALUES (
  'A', 
  '{"attrA":1,"attrB":true,"attrC":["a", "b", "c"]}'
);
Run Code Online (Sandbox Code Playgroud)

现在,我需要attrB从列中删除data.

alter table my_table drop column data->'attrB';这样的东西会很好.但是使用临时表的方法也足够了.

sql postgresql json postgresql-json

46
推荐指数
6
解决办法
3万
查看次数

与Postgresql JSON数据列不同

试图在带有rails的模式上做区别.

2.1.1 :450 > u.profiles.select("profiles.*").distinct


Profile Load (0.9ms)  SELECT DISTINCT profiles.* FROM "profiles" INNER JOIN "integration_profiles" ON "profiles"."id" = "integration_profiles"."profile_id" INNER JOIN "integrations" ON "integration_profiles"."integration_id" = "integrations"."id" WHERE "integrations"."user_id" = $1  [["user_id", 2]]
PG::UndefinedFunction: ERROR:  could not identify an equality operator for type json
LINE 1: SELECT DISTINCT profiles.* FROM "profiles" INNER JOIN "integ...
                        ^
: SELECT DISTINCT profiles.* FROM "profiles" INNER JOIN "integration_profiles" ON "profiles"."id" = "integration_profiles"."profile_id" INNER JOIN "integrations" ON "integration_profiles"."integration_id" = "integrations"."id" WHERE "integrations"."user_id" = $1
ActiveRecord::StatementInvalid: …
Run Code Online (Sandbox Code Playgroud)

postgresql ruby-on-rails ruby-on-rails-3 postgresql-json

38
推荐指数
3
解决办法
2万
查看次数

PostgreSQL中的JSON Schema验证?

我在PostgreSQL中找不到有关JSON模式验证的任何信息,有没有办法在PostgreSQL JSON数据类型上实现JSON模式验证?

postgresql json jsonschema postgresql-json

24
推荐指数
2
解决办法
7782
查看次数

在PostgreSQL 9.3中为json字段的嵌套属性创建索引的多种方法

在PostgreSQL 9.3中,有多种方法可以构建一个表达式,它指向一个json字段的嵌套属性:

data->'foo'->>'bar'
data#>>'{foo,bar}'
json_extract_path_text(data, 'foo', 'bar')
Run Code Online (Sandbox Code Playgroud)

因此,如果查询的表达式与索引的表达式完全匹配,则PostgreSQL仅使用这些索引.

CREATE TABLE json_test_index1(data json);
CREATE TABLE json_test_index2(data json);
CREATE TABLE json_test_index3(data json);

CREATE INDEX ON json_test_index1((data->'foo'->>'bar'));
CREATE INDEX ON json_test_index2((data#>>'{foo,bar}'));
CREATE INDEX ON json_test_index3((json_extract_path_text(data, 'foo', 'bar')));

-- these queries use an index, while all other combinations not:

EXPLAIN SELECT * FROM json_test_index1 WHERE data->'foo'->>'bar' = 'baz';
EXPLAIN SELECT * FROM json_test_index2 WHERE data#>>'{foo,bar}' = 'baz';
EXPLAIN SELECT * FROM json_test_index3 WHERE json_extract_path_text(data, 'foo', 'bar') = 'baz';
Run Code Online (Sandbox Code Playgroud)

我的问题是:

这种行为是有意的吗?我认为查询优化器应该(至少)使用带有#>>运算符的索引,当查询包含适当的调用时json_extract_path_text()- …

postgresql indexing json postgresql-9.3 postgresql-json

7
推荐指数
1
解决办法
2409
查看次数

使用row_to_json进行Postgres递归查询

我在postgres 9.3.5中有一个表格,如下所示:

CREATE TABLE customer_area_node
(
  id bigserial NOT NULL,
  customer_id integer NOT NULL,
  parent_id bigint,
  name text,
  description text,

  CONSTRAINT customer_area_node_pkey PRIMARY KEY (id)
)
Run Code Online (Sandbox Code Playgroud)

我查询:

WITH RECURSIVE c AS (
       SELECT *, 0 as level, name as path FROM customer_area_node WHERE customer_id = 2 and parent_id is null
       UNION ALL
       SELECT customer_area_node.*, 
       c.level + 1 as level, 
       c.path || '/' || customer_area_node.name as path
  FROM customer_area_node 
  join c ON customer_area_node.parent_id = c.id
)
SELECT * FROM c ORDER …
Run Code Online (Sandbox Code Playgroud)

sql postgresql recursion json postgresql-json

6
推荐指数
2
解决办法
4230
查看次数

尝试 ORDER BY 时间戳时 ARRAY_AGG 被 GROUP BY 中断

我为我的问题准备了一个SQL Fiddle -

给出下表:

CREATE TABLE chat(
    gid integer,            /* game id */
    uid integer,            /* user id */
    created timestamptz,
    msg text
);
Run Code Online (Sandbox Code Playgroud)

填充以下测试数据:

INSERT INTO chat(gid, uid, created, msg) VALUES
    (10, 1, NOW() + interval '1 min', 'msg 1'),
    (10, 2, NOW() + interval '2 min', 'msg 2'),
    (10, 1, NOW() + interval '3 min', 'msg 3'),
    (10, 2, NOW() + interval '4 min', 'msg 4'),
    (10, 1, NOW() + interval '5 min', 'msg 5'),
    (10, 2, …
Run Code Online (Sandbox Code Playgroud)

sql postgresql sql-order-by postgresql-json array-agg

5
推荐指数
1
解决办法
1319
查看次数

如何使用jpa在postgresql中将继承类列表保存为json?

为了在 postgresql 中保存 json 格式的列表,我com.vladmihalcea:hibernate-types-52在 Spring boot 2 中使用如下:

@Entity
@TypeDefs({
    @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
})
public class Profile {
    ...
    @Type(type = "jsonb")
    @Column(columnDefinition = "jsonb")
    private List<String> locations;

    // getters & setters
}
Run Code Online (Sandbox Code Playgroud)

有了这个,一切都OK了,我可以将位置保存为postgresql中的json。但我想将位置保存为类继承结构。课程如下:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.PROPERTY, property = "type")
@JsonSubTypes({
    @JsonSubTypes.Type(value = SubClass1.class, name = "subClass1"),
    @JsonSubTypes.Type(value = SubClass2.class, name = "subClass2")
})
public abstract class MyClass {
    private String name;
    // getters & setters
}
public class SubClass1 extends …
Run Code Online (Sandbox Code Playgroud)

java jackson spring-boot postgresql-json jackson-databind

5
推荐指数
0
解决办法
1012
查看次数

Postgresql从JSONB字段中的对象数组中获取键

这是 jsonb 列的虚拟数据

[ { "name": [ "sun11", "sun12" ], "alignment": "center", "more": "fields" }, { "name": [ "sun12", "sun13" ], "alignment": "center" }, { "name": [ "sun14", "sun15" ] }]
Run Code Online (Sandbox Code Playgroud)

我想从 jsonb 对象数组中获取所有名称键值...期待输出 -

[ [ "sun11", "sun12" ], [ "sun12", "sun13" ], [ "sun14", "sun15" ] ]
Run Code Online (Sandbox Code Playgroud)

问题是我可以通过提供 0、1 等索引来获取名称键值

SELECT data->0->'name' FROM public."user";
[ "sun11", "sun12" ]
Run Code Online (Sandbox Code Playgroud)

但是我无法从同一个对象数组中获取所有名称键值。我只想从 json 对象数组中获取所有键值。任何帮助都会有所帮助。谢谢

postgresql postgresql-json jsonb

4
推荐指数
1
解决办法
3536
查看次数