Home > OS >  Adding more data in array of object in PostgreSQL
Adding more data in array of object in PostgreSQL

Time:12-15

I have a table of cart with 2 columns (user_num, data).

user_num will have the phone number of user and data will have an array of object like [{ "id": 1, "quantity": 1 }, { "id": 2, "quantity": 2 }, { "id": 3, "quantity": 3 }] here id is product id.

 user_num |                                         data
---------- --------------------------------------------------------------------------------------
        1 | [{ "id": 1, "quantity": 1 }, { "id": 2, "quantity": 2 }, { "id": 3, "quantity": 3 }]

I want to add more data of products in above array of objects in PostgreSQL.

Thanks!

CodePudding user response:

To add the value use the JSONB array append operator ||

Demo

update
  test
set
  data = data || '[{"id": 4, "quantity": 4}, {"id": 5, "quantity": 5}]'
where
  user_num = 1;
  • Related