Samir
Samir

Reputation: 93

Adding more data in array of object in PostgreSQL

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!

Upvotes: 1

Views: 93

Answers (1)

Pooya
Pooya

Reputation: 3183

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;

Upvotes: 1

Related Questions