How do I modify fields inside the new PostgreSQL JSON datatype?

前端 未结 21 2203
孤独总比滥情好
孤独总比滥情好 2020-11-22 15:37

With postgresql 9.3 I can SELECT specific fields of a JSON data type, but how do you modify them using UPDATE? I can\'t find any examples of this in the postgresql documenta

21条回答
  •  长情又很酷
    2020-11-22 16:40

    UPDATE table_name SET attrs = jsonb_set(cast(attrs as jsonb), '{key}', '"new_value"', true) WHERE id = 'some_id';

    This what worked for me, attrs is a json type field. first cast to jsonb then update.

    or

    UPDATE table_name SET attrs = jsonb_set(cast(attrs as jsonb), '{key}', '"new_value"', true) WHERE attrs->>key = 'old_value';

提交回复
热议问题