将新数据添加到jsonb列

时间:2020-06-03 12:17:46

标签: javascript sql postgresql

我有jsonb列,其中包含下一个数据:

{
    'key1': 'data1'
}

带有一些物体:

const myObj = {
   'key2': 'data2'
}

我需要编写SQL查询以将myObj添加到jsonb列中,结果如下:

{
    'key1': 'data1',
    'key2': 'data2'
}

1 个答案:

答案 0 :(得分:1)

使用||运算符,即

update <tablename> 
  set jsonb_column = jsonb_column || '{"key2":"data2"}'::jsonb 
  where ...

这里是documentation