- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图将一个键附加到 postgres 中的嵌套 jsonb,但出现错误。本质上,我从一个 json 开始:
{"tel": "123", "name": "foo", "new_info": {"a": "bar"}}
我想将 {"b", "baz"}
附加到 "new_info"中,这样生成的 jsonb 是:
{"tel": "123", "name": "foo", "new_info": {"a": "bar", "b":"baz"}}
我正在使用以下命令获取原始 jsonb:
CREATE TABLE mytable (
ID serial NOT NULL PRIMARY KEY,
data jsonb NOT NULL
);
INSERT INTO mytable (data)
VALUES
(
'{ "name": "foo", "tel": "123"}'
);
UPDATE mytable SET data = jsonb_set(data, '{new_info}', '{"a":"bar"}', TRUE) WHERE data @> '{"name": "foo"}' ;
并尝试使用以下内容更新不起作用的“new_info”:
WITH orig_new_info AS (SELECT data#>'{new_info}' FROM mytable WHERE data @> '{"name": "foo"}')
WITH updated_new_info AS (jsonb_set(orig_new_info, '{"b":"baz"}',TRUE ))
UPDATE mytable SET data = jsonb_set(data, '{new_info}', updated_new_info, TRUE) WHERE data @> '{"name": "foo"}';
非常感谢任何指点!
更新 #1:
Per klins 回答了以下工作:
update mytable
set data = jsonb_insert(data, '{new_info}', data->'new_info' || '{"b":"baz"}', TRUE)
where data @> '{"name": "foo"}'
returning *;
但是如何避免使用 jsonb_insert 之类的方法覆盖现有 key 。换句话说,为什么以下示例不起作用?:
#ex 1
update mytable
set data = jsonb_insert(data, '{new_info}', jsonb_insert(SELECT data->'new_info' FROM mytable WHERE data @> '{"name": "foo"}'), '{"b":"baz"}'),true)
where data @> '{"name": "foo"}'
returning *;
#ex2
WITH orig_new_info AS (SELECT data#>'{new_info}' FROM mytable WHERE data @> '{"name": "foo"}')
WITH updated_new_info AS(SELECT jsonb_insert(orig_new_info, orig_new_info ||'{"b":"bazer"}'))
update mytable
set data = jsonb_set(data, '{new_info}', updated_new_info, TRUE)
where data @> '{"name": "foo"}'
returning *;
换句话说,klin 的答案只考虑顶级 data
jsonb 中的键,而不是 中嵌套的
。"new_info"
json 的键>数据
更新#2:
Per klins 更新了以下工作的答案:
update mytable
set data = jsonb_insert(data, '{new_info, b}', '"baz"')
where data @> '{"name": "foo"}'
但是,如果 "new_info"
不存在于 data
中,更新将成功完成而不保存。因此以下命令成功完成但数据未保存:
DROP TABLE mytable;
CREATE TABLE mytable (
ID serial NOT NULL PRIMARY KEY,
data jsonb NOT NULL
);
INSERT INTO mytable (data)
VALUES
(
'{ "name": "foo", "tel": "123"}'
);
update mytable
set data = jsonb_insert(data, '{new_info, b}', '"baz"')
where data @> '{"name": "foo"}'
returning *;
所以这对我来说有点令人惊讶,因为它给人的印象是它保存了,但实际上并没有。我想避免使用 case 语句,因为大多数时候这将是不必要的检查,如果“new_info”不存在则宁愿它失败(或者如果它不增加开销到“new_info”已经存在的情况下则创建它存在)。即我想避免这些答案的作用:
Check if key exists in a JSON with PL/pgSQL?
Update or create nested jsonb value using single update command
最佳答案
使用||
(串联运算符):
update mytable
set data = jsonb_set(data, '{new_info}', data->'new_info' || '{"b":"baz"}')
where data @> '{"name": "foo"}'
returning *
id | data
----+---------------------------------------------------------------------
1 | {"tel": "123", "name": "foo", "new_info": {"a": "bar", "b": "baz"}}
(1 row)
UPDATE 1
函数 jsonb_set()
是在 Postgres 9.5 中引入的。在 Postgres 9.6+ 中,您还可以使用 jsonb_insert(),
这可能更直接:
update mytable
set data = jsonb_insert(data, '{new_info, b}', '"baz"')
where data @> '{"name": "foo"}'
jsonb_insert(target jsonb, path text[], new_value jsonb, [insert_after boolean])
(...) If target section designated by path is in JSONB object, new_value will be inserted only if target does not exist.
因此路径必须指向不存在的键(您要插入的键)。
关于sql - 将嵌套键值添加到 Postgres 中的 JSONB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51014045/
我有一个 Postgres 表,mytable 其中一个字段如下: myField JSONB[] NOT NULL 让我们假设上述 json 是这种形式: { "字母":"A", "数字":30}
在 PostgreSQL 的一个 jsonb 字段中保存多个地址是个好主意。我是 nosql 的新手,我想测试 PostgreSQL 来做到这一点。我不想有另一个包含地址的表,我更喜欢将它放在同一个表
我得到了带有 jsonb 的字段 tags: [{"value": "tag1"}] 我需要做这样的事情 update table1 set tags = tags - '{"value": "tag
我使用 postgres9.4,并且存在关系“Patients”具有类型为 jsonb[] 的列“contact”,如何传输类型 jsonb[ ] 到 jsonb? 记录如下。 =>select na
我正在尝试使用以下查询基于 jsonb 字段中的多个 json 属性将 jsonb 字段更新到表中 insert into testtable(data) values('{ "key": "
我有列 options 类型为 jsonb ,格式为 {"names": ["name1", "name2"]} 是用 创建的 UPDATE table1 t1 SET options = (SELE
我在其中一个项目中使用 Postgres 数据库。现在我需要将 JSON 数组存储在数据库中。如下所示: 例如,我有以下 JSON 结构: [ { "Id": 1, "Name":
我正在尝试从 postgre 表中读取一个 jsonb 字段。我尝试通过以下方式使用 Jsonb 库将其作为 Jsonb 类型读取: @Entity @Table(name = "test") dat
tl;dr -- 有什么方法可以从 postgres 中的 jsonb 对象获取值作为 jsonb_array 吗? 尝试在 postgres 中使用递归 cte 来展平任意深度的树结构,如下所示:
给定类似于以下的现有数据结构: CREATE TEMP TABLE sample (id int, metadata_array jsonb, text_id_one jsonb, text_id_t
psql --version psql (PostgreSQL) 9.4.1 rails -v Rails 4.2.0 我通过这样的迁移添加了一个 jsonb 列 class AddPreferenc
假设我有一个模型 Neighborhood,它有一个 jsonb[] 字段 families,它是一个包含 json 对象的数组,具有任何类型的键值对,如下所示 [{"name":"Smiths",
我在这里显示了以下数据集 http://sqlfiddle.com/#!17/f9280/1 我想查询数据的方式是每个键和类别的平均排名 例如, 键 1,类别 10,avg_rank:95 键 1,类
我在 springboot(2.1)+postgres(10.5)+hibernate(5.3.7) 中使用 jsonb。 以下是文件中的更改: 在 pom.xml 中 .... com.vladm
考虑像下面这样的模型和一个 bars 的 json 数组: const myModel = { id: 1, bars: [ { aproperty: 10 },
friend 们好,我需要帮助来解决以下问题, 我在我的 postgres 数据库表中有一组记录,其中表有 JSONB 类型字段。 JSONB 类型列包含以下JSON, 记录#1 :- { "ke
我有一个表,其中 JSONB 列存储 JSONB 数组/字符串(下例中的 value_r 列)。仅对 JSONB 列中的 JSONB 数组的内容进行排序(还存储字符串)的最简单(且有效)的方法是什么?
在 PostgreSQL 9.5 表中,我有一个 integer 列 social。 当我尝试在存储过程中更新它时,在jsonb 类型的 in_users 变量中给定以下 JSON 数据(一个包含 2
我有一个专栏amount_splits我需要按照我指定的键顺序保存我的 JSON。 如何防止 Rails/Postgres jsonb当我将它保存到数据库时自动排序我的 JSON 键? (用于创建或更
我想使用 UNION ALL 运算符将 2 个表合并为一个表。第一个表有几个字段。第二个表将几个字段分组到 JSONB 字段上。 为了简化问题,我使用这个简单的 SQL 请求(不依赖于表)重现了错误:
我是一名优秀的程序员,十分优秀!