简体   繁体   English

为jsonb现有密钥添加值并更新其他postgres

[英]Add value to jsonb existing key and update others postgres

How can I add values to jsonb key ? 如何将值添加到jsonb键? Example ` 范例`

{"drivers": "15","updatedat":"someDate"}

after upd must be 更新后必须

{"drivers":"15,...,26","updatedat":"nowTime"}

updatedat inserted from node,so there is no problem,I can select value of derivers in node,add new and update,Is there other way make it with 1 query without using node? 从节点插入时已更新,所以没有问题,我可以在节点中选择派生类的值,添加新值并进行更新,是否还有其他方法可以在不使用节点的情况下进行1次查询?

If I understand your task correctly, you just need jsonb_set : 如果我正确理解您的任务,则只需jsonb_set

t=# select jsonb_set('{"drivers": "15","updatedat":"someDate"}'::jsonb, '{updatedate}'::text[],to_jsonb(now()));
                                          jsonb_set
----------------------------------------------------------------------------------------------
 {"drivers": "15", "updatedat": "someDate", "updatedate": "2018-05-29T15:49:41.772188+00:00"}
(1 row)

unless you have older version of course... 除非您当然有旧版本...

update to change drivers att value: 更新以更改驱动程序的att值:

t=# with c(j) as (values('{"drivers": "15","updatedat":"someDate"}'::jsonb))
select jsonb_set(j,'{drivers}'::text[],to_jsonb(j->>'drivers'||',...,26')) from c;
                     jsonb_set
---------------------------------------------------
 {"drivers": "15,...,26", "updatedat": "someDate"}
(1 row)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM