PostgreSQL
 sql >> Database >  >> RDS >> PostgreSQL

Converti l'array di oggetti in array compatibile per nodejs/pg/unnest

Puoi inviare la tua stringa JSON così com'è e farla gestire da PostgreSQL:

update portfolios p
set votes = s.votes
from (
  select (e->>'votes')::int as votes, (e->>'id')::int as id
  from (select (regexp_replace($1, '"\1"', 'g'))::jsonb as jarr) j
  cross join jsonb_array_elements(jarr) e
  ) s
where p.id = s.id;

Dove $1 is [{votes: 5, id: 1}, {votes: 15, id: 1}, {votes: 25, id: 2}]', '([a-z]+) come una stringa.