jsonb

JSONB column: sort only content of arrays stored in column with mixed JSONB content

我的梦境 提交于 2019-12-24 14:35:55
问题 I have a table with JSONB column storing JSONB arrays/strings ( value_r column in the below example). What would be the simplest (and efficent) way to sort only content of JSONB arrays within JSONB column (storing also strings)? I've been looking for the simplest method (as query, or procedure is needed?) because I have to apply this in more complicated SQL code. Here is the test code: CREATE TABLE test_table ( id integer, ordinality bigint, key_r text, value_r jsonb ); INSERT INTO test_table

Speed up range test for key values nested in jsonb array of objects

守給你的承諾、 提交于 2019-12-24 10:05:49
问题 Suppose I have the following parents table: create table parents ( id integer not null constraint parents_pkey primary key, name text not null, children jsonb not null ); Where children is a json array of the following structure: [ { "name": "child1", "age": 10 }, { "name": "child2", "age": 12 } ] And I need, for example, to get all parents that have children with age between 10 and 12. I create the following query: select distinct p.* from parents p, jsonb_array_elements(p.children) c where

Searching for a particular value in a jsonb datatype prostgresql

假如想象 提交于 2019-12-24 06:16:05
问题 We are storing dynamic json data to a postgresql jsonb datatype field. Most of the json keys are dynamic and nested. Now I need to search one particular value inside this json data field. This value may be present with any key name. row1 = { "header": { "piecesids" : [100,200,300] }, "footer" : {"pieceids" : [500,300]} } row2 = { "header_right": { "piecesids" : [500,300,400] }, "footer_left" : {"pieceids" : [300,200]} } row3 = { "body_right": { "piecesids" : [500,300,600] }, "body_left" : {

Postgres 9.6 - Create unique index for json array

南楼画角 提交于 2019-12-24 03:42:51
问题 'Sup, We have a Postgres (9.6) table myTable with a row data jsonb NOT NULL : DROP TABLE myTable; CREATE TABLE myTable (data jsonb NOT NULL); We want to store object containing an array of email addresses: INSERT INTO myTable (data) VALUES ($${"email": [{"address": "A"}, {"address": "B"}]}$$); INSERT INTO myTable (data) VALUES ($${"email": [{"address": "C"}]}$$); We want to create a unique index on address to prevent insertion of row like: -- "A" NON UNIQUE, SHOULD FAIL INSERT INTO myTable

Postgres jsonb array: query for non-empty intersection

馋奶兔 提交于 2019-12-23 18:56:40
问题 Suppose I have a JSONB column called value in a table t , and inside of these blobs of JSON is a tags field which is a list of strings. I'd like to make a query for any of these JSON blobs tagged "foo" or "bar" . So suppose the table data looks like this: value --------------------- {"tags": ["other"]} {"tags": ["foo", "quux"]} {"tags": ["baz", "bar"]} {"tags": ["bar", "foo"]} {"tags": []} I want to write some sort of query like this: select value from t where value->'tags' NONEMPTY

How to join jsonb array elements in Postgres?

强颜欢笑 提交于 2019-12-23 13:12:47
问题 I am using Postgres 9.5, and I have the following tables: Users id UUID name TEXT Images id UUID key TEXT width INTEGER height INTEGER Posts id UUID title TEXT author_id UUID content JSONB The posts' content is like: [ { "type": "text", "text": "learning pg" }, { "type": "image", "image_id": "8f4422b4-3936-49f5-ab02-50aea5e6755f" }, { "type": "image", "image_id": "57efc97c-b9b4-4cd5-b1e1-3539f5853835" }, { "type": "text", "text": "pg is awesome" } ] Now I want to join the image type of

How can I perform a LIKE query for a jsonb key?

…衆ロ難τιáo~ 提交于 2019-12-23 07:38:53
问题 I have the following jsonb structure: {'this': '1', 'this_that': '0', 'this_and_that': '5'} How do I select rows that contain a LIKE operator? SELECT * FROM myjson WHERE j ? 'this_%' Returns 0 rows...was hoping it would match 'this_that' and 'this_and_that'. (Note: the characters following '_' will potentially vary greatly and therefore I am not able to do an exact match). 回答1: Your example should not work because there are not implicit cast between jsonb and text types. You can enforce

How to escape the ? (question mark) operator to query Postgresql JSONB type in Rails

馋奶兔 提交于 2019-12-23 06:49:35
问题 I'm working with Rails 4.2 and Postgres 9.4 to try out the new JSONB data type. One of the JSONB columns in my database holds an array, and I want to be able to query for records where this array contains a certain value. I figured out how to do this using the new JSONB "question mark" ("contains") operator, as documented here: http://www.postgresql.org/docs/9.4/static/functions-json.html So in raw SQL I can get this to work as in this example: SELECT * FROM people WHERE roles ? '32486d83

How do you create a Postgresql JSONB array in array index?

帅比萌擦擦* 提交于 2019-12-22 12:32:39
问题 I have structure like this: user_id, a. a is of type jsonb and has the following structure: { b: [ {ids: [1,2,3,4]}, {ids: [2,3,4]}, {ids: [1,2,4]}, ... ] } How would I make an index that enabled me to find all users (user_id) that has a certain id in the ids list? 回答1: Is a GIN index what you want? It seems that you first need to organized the IDs into a form that is more tractable. I'm more familiar with Python than I am with the PostgreSQL ways of manipulating JSON, so I used PL/Python for

How do you create a Postgresql JSONB array in array index?

谁都会走 提交于 2019-12-22 12:32:15
问题 I have structure like this: user_id, a. a is of type jsonb and has the following structure: { b: [ {ids: [1,2,3,4]}, {ids: [2,3,4]}, {ids: [1,2,4]}, ... ] } How would I make an index that enabled me to find all users (user_id) that has a certain id in the ids list? 回答1: Is a GIN index what you want? It seems that you first need to organized the IDs into a form that is more tractable. I'm more familiar with Python than I am with the PostgreSQL ways of manipulating JSON, so I used PL/Python for