Search code examples
postgresqljsonbpostgresql-10

How can I do less than, greater than in JSON array object Postgres fields?


I want to retrieve data by specific field operation it store array of object. i want to add new object in it.

CREATE TABLE justjson ( id INTEGER, doc JSONB);
INSERT INTO justjson VALUES ( 1, '[
  {
    "name": "abc",
    "age": "22"
  },
  {
    "name": "def",
    "age": "23"
  }
]');

retrieve data where age is greater then and equal to 23 how is possible


Solution

  • eg using jsonb_array_elements:

    t=# with a as (select *,jsonb_array_elements(doc) k from justjson)
    select k from a where (k->>'age')::int >= 23;
                  k
    ------------------------------
     {"age": "23", "name": "def"}
    (1 row)