Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

373
Views
¿Cómo insertar valor en la clave [] en la columna jsonb?

Tengo tal estructura jsonb en la tabla de Postgres: { "res": [123, 223] } y quiero insertar valores en res -array. No sé cuántos valores hay en la matriz y como jsonb_insert opera en la posición, probé con la posición -1 :

 select jsonb_insert( '{"res": [123, 223]}', '{res,-1}', '333'); jsonb_insert -------------------------- {"res": [123, 333, 223]}

No funciona. ¿Cómo empujar nuevos valores al final de la matriz?

Estoy usando Postgres 9.6

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Use el índice -1 junto con insert_after = true :

 SELECT jsonb_insert( '{"res": [123, 456, 789, 101112]}', '{res, -1}', '333', true); +-------------------------------------+ |jsonb_insert | +-------------------------------------+ |{"res": [123, 456, 789, 101112, 333]}| +-------------------------------------+

El valor predeterminado de insert_after es false , lo que significa que, aunque apunta al último elemento (índice = -1), termina insertando en la penúltima posición:

 SELECT jsonb_insert( '{"res": [123, 456, 789, 101112]}', '{res, -1}', '333', false); +-------------------------------------+ |jsonb_insert | +-------------------------------------+ |{"res": [123, 456, 789, 333, 101112]}| +-------------------------------------+
over 4 years ago · Santiago Trujillo Report

0

Una posible solución es usar una posición ridículamente grande:

 select jsonb_insert( '{"res": [123, 223]}', '{res,1000000}', '333'); jsonb_insert -------------------------- {"res": [123, 223, 333]}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!