jsonb

Query for array elements inside JSON type

女生的网名这么多〃 提交于 2019-11-26 06:49:40
I'm trying to test out the json type in PostgreSQL 9.3. I have a json column called data in a table called reports . The JSON looks something like this: { "objects": [ {"src":"foo.png"}, {"src":"bar.png"} ], "background":"background.png" } I would like to query the table for all reports that match the 'src' value in the 'objects' array. For example, is it possible to query the DB for all reports that match 'src' = 'foo.png' ? I successfully wrote a query that can match the "background" : SELECT data AS data FROM reports where data->>'background' = 'background.png' But since "objects" has an

Explanation of JSONB introduced by PostgreSQL

为君一笑 提交于 2019-11-26 04:03:35
问题 PostgreSQL just introduced JSONB and it\'s already trending on hacker news. It would be great if someone could explain how it\'s different from Hstore and JSON previously present in PostgreSQL. What are it\'s advantages and limitations and when should someone consider using it? 回答1: First, hstore is a contrib module, which only allows you to store key => value pairs, where keys and values can only be text s (however values can be sql NULL s too). Both json & jsonb allows you to store a valid

Flatten aggregated key/value pairs from a JSONB field?

99封情书 提交于 2019-11-26 03:40:02
问题 I am working in Postgres 9.4 with the following table: Column │ Type │ Modifiers ─────────────────┼──────────────────────┼────────────────────── id │ integer │ not null default practice_id │ character varying(6) │ not null date │ date │ not null pct_id │ character varying(3) │ total_list_size │ double precision │ not null star_pu │ jsonb │ I have the following query: SELECT date, AVG(total_list_size) AS total_list_size, json_object_agg(key, val) AS star_pu FROM (SELECT date, SUM(total_list

Query for array elements inside JSON type

房东的猫 提交于 2019-11-26 01:51:39
问题 I\'m trying to test out the json type in PostgreSQL 9.3. I have a json column called data in a table called reports . The JSON looks something like this: { \"objects\": [ {\"src\":\"foo.png\"}, {\"src\":\"bar.png\"} ], \"background\":\"background.png\" } I would like to query the table for all reports that match the \'src\' value in the \'objects\' array. For example, is it possible to query the DB for all reports that match \'src\' = \'foo.png\' ? I successfully wrote a query that can match

Index for finding an element in a JSON array

家住魔仙堡 提交于 2019-11-26 01:23:41
问题 I have a table that looks like this: CREATE TABLE tracks (id SERIAL, artists JSON); INSERT INTO tracks (id, artists) VALUES (1, \'[{\"name\": \"blink-182\"}]\'); INSERT INTO tracks (id, artists) VALUES (2, \'[{\"name\": \"The Dirty Heads\"}, {\"name\": \"Louis Richards\"}]\'); There\'s several other columns that aren\'t relevant to this question. There\'s a reason to have them stored as JSON. What I\'m trying to do is lookup a track that has a specific artist name (exact match). I\'m using