jsonb

Update JSONB column where JSONB column has certain value postgreSQL

跟風遠走 提交于 2020-01-16 09:00:16
问题 I have a table named guest_group in a Postgres DB and a column named custom_fields with the following values: (example) [ { "value":"Frau Barbara Test", "display_name":"Name", "servicio_tags":[ "full-name" ] }, { "value":"118", "display_name":"Zimmernummer", "servicio_tags":[ "room-number" ] }, { "value":"likes postgreSQL", "display_name":"Traces", "servicio_tags":[ "trace" ] } ] I want to update all rows with the value equals to Frau Barbara Test and where the servicio_tags array is equals

How to filter a value of any key of json in postgres

家住魔仙堡 提交于 2020-01-16 04:21:09
问题 I have a table users with a jsonb field called data . I have to retrieve all the users that have a value in that data column matching a given string. For example: user1 = data: {"property_a": "a1", "property_b": "b1"} user2 = data: {"property_a": "a2", "property_b": "b2"} I want to retrieve any user that has a value data matching 'b2' , in this case that will be 'user2' . Any idea how to do this in an elegant way? I can retrieve all keys from data of all users and create a query manually but

Use where queries in JSONB datatype in Rails Postgres

家住魔仙堡 提交于 2020-01-15 18:50:48
问题 How to query out schedules who has slot_details which have empty appointment_id. Here is an example data set: id: 98, consulting_location_doctor_id: 498, schedule_date: Thu, 15 Jan 2015 00:00:00 IST +05:30, slot_details: [{"end"=>"2015-01-15T15:00:00.000+00:00", "start"=>"2015-01-15T14:30:00.000+00:00", "title"=>" 2:30 pm to 3:00 pm", appointment_id: ""}, {"end"=>"2015-01-15T15:30:00.000+00:00", "start"=>"2015-01-15T15:00:00.000+00:00", "title"=>" 3:00 pm to 3:30 pm", appointment_id: ""}, {

Flattening JSONB array in postgres

北战南征 提交于 2020-01-14 04:52:30
问题 I am using Postgres 9.4 and storing my data in as JSONB arrays. I am looking for a way to extract json elements inside the array and replace them with one concatenated json element using psql. Consider as example following table: 'aaa' | [{"a":"foo"},{"b":"bar"},{"c":["baz", 'boom']}] | 404 'bbb' | [{"bar":"foo"}] | 501 What I am looking to achieve is: 'aaa' | {"a":"foo", "b":"bar", "c":["baz", "boom"]} | 404 'bbb' | {"bar":"foo"} | 501 I have tried to achieve it using builtin postgres

Merging JSONB values in PostgreSQL?

拈花ヽ惹草 提交于 2020-01-09 03:54:04
问题 Using the || operator yields the following result: select '{"a":{"b":2}}'::jsonb || '{"a":{"c":3}}'::jsonb ; ?column? ----------------- {"a": {"c": 3}} (1 row) I would like to be able to do achieve the following result ( ?? just a placeholder for the operator): select '{"a":{"b":2}}'::jsonb ?? '{"a":{"c":3}}'::jsonb ; ?column? ----------------- {"a": {"b": 2, "c": 3}} (1 row) So, you can see the top-level a key has its child values "merged" such that the result contains both b and c . How do

Does the phrase search operator <-> work with JSONB documents or only relational tables?

一曲冷凌霜 提交于 2020-01-05 08:10:59
问题 Does the phrase search operator <-> work with JSONB documents or only relational tables in PostgreSQL? I haven't experimented with this yet, as I haven't yet set up Postgres hosting. The answer to this question will help determine what database and what tools I will be using. I found this sample code at: https://compose.com/articles/mastering-postgresql-tools-full-text-search-and-phrase-search/: SELECT document_id, document_text FROM documents WHERE document_tokens @@ to_tsquery('jump <->

Is ISO8601 the best date-format for PostgreSQL jsonb when i want to filter by the date?

依然范特西╮ 提交于 2020-01-05 06:29:28
问题 I'm new to PostgreSQL and I have the following question: I have a table with just an id-column and a data-column, which uses the jsonb-type. Inside the jsonb-object I have a datetime field. I read in various posts, that I should use the ISO-8601 dateformat to store in the DB. I want to filter my table by date like this: SELECT * FROM table WHERE data->'date' > '2016-01-01T00:00' Is this really the best date-format for this purpose? Thanks in advance :) 回答1: The big advantage of that format is

LIKE query on elements of flat jsonb array

自闭症网瘾萝莉.ら 提交于 2020-01-04 09:29:24
问题 I have a Postgres table posts with a column of type jsonb which is basically a flat array of tags. What i need to do is to somehow run a LIKE query on that tags column elements so that i can find a posts which has a tags beginning with some partial string. Is such thing possible in Postgres? I'm constantly finding super complex examples and no one is ever describing such basic and simple scenario. My current code works fine for checking if there are posts having specific tags: select * from

“invalid reference to FROM-clause entry for table” in Postgres query

时光毁灭记忆、已成空白 提交于 2020-01-03 20:12:09
问题 I have the following query: query = "SELECT data #>> '{id}' AS id, data #>> '{name}' AS name, data #>> '{curator}' AS curator, data #> '{$isValid}' AS \"$isValid\", data #> '{customer}' AS customer, data #> '{$createdTS}' AS \"$createdTS\", data #> '{$updatedTS}' AS \"$updatedTS\", data #> '{$isComplete}' AS \"$isComplete\", (count(keys))::numeric as \"numProducts\", created_at FROM appointment_intakes, LATERAL jsonb_object_keys(data #> '{products}') keys INNER JOIN appointment_intake_users

Postgres jsonb search in array with greater operator (with jsonb_array_elements)

雨燕双飞 提交于 2020-01-02 12:20:25
问题 I try to search a solution but I didn't find anything for my case... Here is the database declaration (simplified): CREATE TABLE documents ( document_id int4 NOT NULL GENERATED BY DEFAULT AS IDENTITY, data_block jsonb NULL ); And this is an example of insert. INSERT INTO documents (document_id, data_block) VALUES(878979, {"COMMONS": {"DATE": {"value": "2017-03-11"}}, "PAYABLE_INVOICE_LINES": [ {"AMOUNT": {"value": 52408.53}}, {"AMOUNT": {"value": 654.23}} ]}); INSERT INTO documents (document