jsonb

What's the proper index for querying structures in arrays in Postgres jsonb?

旧街凉风 提交于 2019-11-27 00:40:38
I'm experimenting with keeping values like the following in a Postgres jsonb field in Postgres 9.4: [{"event_slug":"test_1","start_time":"2014-10-08","end_time":"2014-10-12"}, {"event_slug":"test_2","start_time":"2013-06-24","end_time":"2013-07-02"}, {"event_slug":"test_3","start_time":"2014-03-26","end_time":"2014-03-30"}] I'm executing queries like: SELECT * FROM locations WHERE EXISTS ( SELECT 1 FROM jsonb_array_elements(events) AS e WHERE ( e->>'event_slug' = 'test_1' AND ( e->>'start_time' >= '2014-10-30 14:04:06 -0400' OR e->>'end_time' >= '2014-10-30 14:04:06 -0400' ) ) ) How would I

PostgreSQL是不是你的下一个JSON数据库?

蓝咒 提交于 2019-11-26 20:01:15
根据 Betteridge 定律(任何头条的设问句可以用一个词来回答:不是),除非你的JSON数据很少修改,并且查询很多。 最新版的PostgreSQL添加更多对JSON的支持,我们曾经问过PostgreSQL是否可以替换MongoDB作为JSON数据库,答案显而易见,但我们更希望的是,啊哈,这个问题由读者来问了。 “PostgreSQL不是已经有一些json的支持了吗?” 是的,在PostgreSQL 9.4之前的版本也有JSON 数据类型了,你可以这样: CREATE TABLE justjson ( id INTEGER, doc JSON) >INSERT INTO justjson VALUES ( 1, '{ "name":"fred", "address":{ "line1":"52 The Elms", "line2":"Elmstreet", "postcode":"ES1 1ES" } }'); 保存了JSON的原始文本到数据库,包括空白行和键顺序及重新的键,我们来查看下保存的数据: >SELECT * FROM justjson; id | doc ----+--------------------------------- 1 | { + | "name":"fred", + | "address":{ + | "line1":"52 The Elms",

How to perform update operations on columns of type JSONB in Postgres 9.4

混江龙づ霸主 提交于 2019-11-26 18:28:29
Looking through the documentation for the Postgres 9.4 datatype JSONB, it is not immediately obvious to me how to do updates on JSONB columns. Documentation for JSONB types and functions: http://www.postgresql.org/docs/9.4/static/functions-json.html http://www.postgresql.org/docs/9.4/static/datatype-json.html As an examples, I have this basic table structure: CREATE TABLE test(id serial, data jsonb); Inserting is easy, as in: INSERT INTO test(data) values ('{"name": "my-name", "tags": ["tag1", "tag2"]}'); Now, how would I update the 'data' column? This is invalid syntax: UPDATE test SET data->

Postgresql query array of objects in JSONB field

穿精又带淫゛_ 提交于 2019-11-26 17:35:50
问题 I have a table in a postgresql 9.4 database with a jsonb field called receivers. Some example rows: [{"id": "145119603", "name": "145119603", "type": 2}] [{"id": "1884595530", "name": "1884595530", "type": 1}] [{"id": "363058213", "name": "363058213", "type": 1}] [{"id": "1427965764", "name": "1427965764", "type": 1}] [{"id": "193623800", "name": "193623800", "type": 0}, {"id": "419955814", "name": "419955814", "type": 0}] [{"id": "624635532", "name": "624635532", "type": 0}, {"id":

postgresql 9.5 using jsonb_set for updating specific jsonb array value

…衆ロ難τιáo~ 提交于 2019-11-26 17:01:58
问题 Currently I am working with postgreSQL 9.5 and try to update a value inside an array of a jsonb field. But I am unable to get the index of the selected value My table just looks like: CREATE TABLE samples ( id serial, sample jsonb ); My JSON looks like: {"result": [ {"8410": "ABNDAT", "8411": "Abnahmedatum"}, {"8410": "ABNZIT", "8411": "Abnahmezeit"}, {"8410": "FERR_R", "8411": "Ferritin"} ]} My SELECT statement to get the correct value works: SELECT id, value FROM samples s, jsonb_array

PostgreSQL jsonb, `?` and JDBC

一个人想着一个人 提交于 2019-11-26 16:56:09
问题 I am using PostgreSQL 9.4 and the awesome JSONB field type. I am trying to query against a field in a document. The following works in the psql CLI SELECT id FROM program WHERE document -> 'dept' ? 'CS' When I try to run the same query via my Scala app, I'm getting the error below. I'm using Play framework and Anorm, so the query looks like this SQL(s"SELECT id FROM program WHERE document -> 'dept' ? {dept}") .on('dept -> "CS") .... SQLException: : No value specified for parameter 5.

Explanation of JSONB introduced by PostgreSQL

霸气de小男生 提交于 2019-11-26 15:35:54
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? pozs 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 JSON value (defined in its spec ). F.ex. these are valid JSON representations: null , true , [1,false,

Flatten aggregated key/value pairs from a JSONB field?

你离开我真会死。 提交于 2019-11-26 14:38:22
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_size) AS total_list_size, key, SUM(value::numeric) val FROM frontend_practicelist p, jsonb_each_text(star

What's the proper index for querying structures in arrays in Postgres jsonb?

落爺英雄遲暮 提交于 2019-11-26 09:28:36
问题 I\'m experimenting with keeping values like the following in a Postgres jsonb field in Postgres 9.4: [{\"event_slug\":\"test_1\",\"start_time\":\"2014-10-08\",\"end_time\":\"2014-10-12\"}, {\"event_slug\":\"test_2\",\"start_time\":\"2013-06-24\",\"end_time\":\"2013-07-02\"}, {\"event_slug\":\"test_3\",\"start_time\":\"2014-03-26\",\"end_time\":\"2014-03-30\"}] I\'m executing queries like: SELECT * FROM locations WHERE EXISTS ( SELECT 1 FROM jsonb_array_elements(events) AS e WHERE ( e->>\

How to query a json column for empty objects?

落爺英雄遲暮 提交于 2019-11-26 07:38:56
问题 Looking to find all rows where a certain json column contains an empty object, {} . This is possible with JSON arrays, or if I am looking for a specific key in the object. But I just want to know if the object is empty. Can\'t seem to find an operator that will do this. dev=# \\d test Table \"public.test\" Column | Type | Modifiers --------+------+----------- foo | json | dev=# select * from test; foo --------- {\"a\":1} {\"b\":1} {} (3 rows) dev=# select * from test where foo != \'{}\';