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

137
Views
Compruebe si Postgres jsonb contiene uno de los valores

Necesito verificar si el campo jsonb puede ser una matriz o simplemente una cadena. Datos simples que tengo:

 INSERT INTO users (jsonb) VALUES ('{"name":"Henry", "favoritefood_1":["eggs","apples"]}'), ('{"name":"Herald","favoritefood_1":["apples","potatoes"]}'), ('{"name":"Helen", "favoritefood_1":"apples"}');

¿Cómo debo encontrar usuarios que aman las manzanas y los huevos? Intenté muchas consultas y la última de ellas es:

 SELECT * FROM users WHERE CASE WHEN jsonb_typeof((jsonb->>'favoritefood_1')::jsonb) = 'array' THEN jsonb_array_elements((jsonb->>'favoritefood_1')::jsonb) IN ('apples', 'eggs') ELSE (jsonb->>'favoritefood_1')::jsonb IN ('apples', 'eggs') END

¿Alguien puede ayudarme con eso, por favor?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

¿Solo ? Deberia trabajar:

 where jsonb -> 'favoritefood_1' ? 'apples'
over 4 years ago · Santiago Trujillo Report

0

where jsonb -> 'favoritefood_1' ?| array['apples', 'eggs']
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!