jsonb

Recursive JSONB postgres

假如想象 提交于 2019-12-11 08:43:26
问题 I am trying to build a recursive CTE in Postgres that supports both arrays and objects, to return a list of key-value pairs and don't seem to be able to find a good example. This is my current code. with recursive jsonRecurse as ( select j.key as Path ,j.key ,j.value from jsonb_each(to_jsonb('{ "key1": { "key2": [ { "key3": "test3", "key4": "test4" } ] }, "key5": [ { "key6": [ { "key7": "test7" } ] } ] }'::jsonb)) j union all select jr.path || '.' || jr2.Key ,jr2.key ,jr2.value from

How to delete array element in JSONB column based on nested key value?

烂漫一生 提交于 2019-12-11 06:08:59
问题 How can I remove an object from an array, based on the value of one of the object's keys? The array is nested within a parent object. Here's a sample structure: { "foo1": [ { "bar1": 123, "bar2": 456 }, { "bar1": 789, "bar2": 42 } ], "foo2": [ "some other stuff" ] } Can I remove an array element based on the value of bar1 ? I can query based on the bar1 value using: columnname @> '{ "foo1": [ { "bar1": 123 } ]}' , but I've had no luck finding a way to remove { "bar1": 123, "bar2": 456 } from

Approaches to dynamically assemble PostgreSQL jsonb queries without string concatenation

泪湿孤枕 提交于 2019-12-11 06:07:35
问题 I would like to query a jsonb column in a PostgreSQL databse from a Java / Spring Boot application. The structure of the jsonb documents is not known beforehand, although all the documents will have the same structure. The structure of the documents in the jsonb column is described in database tables. The application obtains the jsonb document structure description from the database. The next step is running analytical queries on the jsonb data. The analytical queries have to be assembled

Improve ranking times on multiple JSONB fields search in PostgreSQL

a 夏天 提交于 2019-12-11 04:45:46
问题 My search times are actually quite fast now but as soon as I start to rank them for the best results I hit a wall. The more hits I get, the slower it gets. For uncommon terms the search takes ~2ms and for more common ones it's ~900ms+. In the example I have gathered all possible structures within my data (simple, arrays, nested arrays). CREATE TABLE book ( id BIGSERIAL NOT NULL, data JSONB NOT NULL ); Then I build a function which concatenate the name values of my nested array field 'author':

How to query Postgres JSONB arrays with = operator

我的未来我决定 提交于 2019-12-11 04:28:37
问题 I'm looking for a way to query postgres jsonb array field with kind of = clause. Let's assume I have a table CREATE TABLE events( id integer, tags jsonb, PRIMARY KEY(id) ); tags having values like ['Google', 'Hello World', 'Ruby'] I have gone through Stackover Flow and done similar things. And the SQL formed is like SELECT "events".* FROM "events" WHERE ("events"."tags" @> '{google}') ORDER BY "business_events"."id" desc; By running this, I'm getting this error => ERROR: invalid input syntax

Update nested key with postgres json field in Rails

烈酒焚心 提交于 2019-12-11 04:24:40
问题 I've been trying to update the following : {"boxes": {"book": 2, "moving": 2}, "goods": {}} to : {"boxes": {"book_new": 2, "moving": 2}, "goods": {}} without using a regular expression or doing this in ruby. but it seems it's a bit tricky. I want to add new key and then delete the old one, I'm not familiar with the syntax and I couldn't find much on the internet. I was able to add a new element to the data but not to the nested boxes!! like that: Update moves SET data = data::jsonb || '{

org.hibernate.MappingException: No Dialect mapping for JDBC type: 1111 for jsonobject with custom JsonPostgreSQLDialect

耗尽温柔 提交于 2019-12-11 03:35:59
问题 1) I added custom dialect by extending PostgreSQL9Dialect which does this.registerColumnType(Types.JAVA_OBJECT, "jsonb"); 2) public class JSONObjectUserType implements UserType { } 3) property name="xxxx" type="org.JSONObjectUserType" length="1000" 4) I run hibernate native SQL query. SQLQuery query1 = (SQLQuery) session.createSQLQuery(str.toString()); It is giving No Dialect mapping for JDBC type: 1111. str = "Select state from X" Postgresql - column state is of type jsonb. 来源: https:/

How to get elements with a unique number from a json array in PostgreSQL?

和自甴很熟 提交于 2019-12-11 03:18:49
问题 I have a table bank_accounts : Column | Type | Modifiers | Storage | Stats target | Description ---------------+-----------------------+-------------------------------------------------------------------------+----------+--------------+------------- id | integer | not null default nextval('bank_accounts_id_seq'::regclass) | plain | | name | character varying(50) | | extended | | bank_accounts | jsonb | not null | extended | | And it has some JSON in the jsonb column: id | name | bank_accounts

Rails ActiveRecord: How to use bind variables with double quotes on jsonb

左心房为你撑大大i 提交于 2019-12-11 02:19:19
问题 I have a postgres jsonb query as follows: Blog.where("upload_data @> '[ { \"name\": \"#{name}\" }]'") Which works but breaks build because the excellent brakeman points out the possible sql injection risk. If I use bind variables: Blog.where("upload_data @> '[ { \"name\": ? }]'", name) It creates a sql query like: WHERE upload_data @> '[ { "name": 'name' }]' Note the single quotes - which is an invalid query If I use single quotes I get: WHERE upload_data @> "[ { 'name': 'name' }]" which is

SQLAlchemy Pandas read_sql from jsonb

烂漫一生 提交于 2019-12-11 01:46:45
问题 I want to generate a dataframe with pandas read_sql from my sqlalchemy query with a PostgreSQL's jsonb attribute to columns. Actually this will give me my answer: query = session.query( cls.id, cls._my_jsonb_column ).all() pd.DataFrame.from_dict([dict(id=id_, **i) for id_,i in query]) But I would prefer to unpack the jsonb with PostgreSQL instead of in the application. My attempt gives query = session.query( cls.id, func.jsonb_to_record(cls._my_jsonb_column) ) pd.read_sql(query.statement,