更新多个jsonb值 [英] Update multiple jsonb values

查看:186
本文介绍了更新多个jsonb值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个cron,以便在具有postgres数据库的ndoe应用程序中每月运行一次以更新jsonb字段的某些键。

I'm trying to create a cron to run every month to update some keys of a jsonb field, in a ndoe application with postgres database.

在我的数据库中我有一个jsonb字段,其中包含 disk_alert, temepratures_alert, consumptions_alert之类的键,并且我想做类似的事情

In my database I have a jsonb field with keys like "disk_alert", "temepratures_alert", "consumptions_alert" and I want to do something like

Postgres:

UPDATE devices SET    
    data=jsonb_set(data::jsonb,'{disk_alert}','false'::jsonb,true), 
    modified_date=NOW() 
WHERE id=$1 AND NOT deleted;

节点:

client.query("UPDATE devices SET data=jsonb_set(data::jsonb,'{disk_alert}','false'::jsonb,true), modified_date=NOW() WHERE id=$1 AND NOT deleted", [deviceId]

但是如何添加其他键 consumptions_alert和 temperatures_alert?我读了一些有关使用cocnatenation ||运算符的内容,但是该怎么做呢?

But how to add the other keys "consumptions_alert" and "temperatures_alert" ?? I read something about to use cocnatenation || operator but how to do this?

推荐答案

您可以使用串联运算符像这样:

You can use the concatenation operator in the way like this:

UPDATE devices SET 
    data = data::jsonb || '{"disk_alert": false, "temepratures_alert": false}',
    modified_date = NOW() 
WHERE id=$1 AND NOT deleted;

这篇关于更新多个jsonb值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆